in Education by
I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to do is check if the value of myArgument is the default value for that type, something like this: if (myArgument == default(T)) But this doesn't compile because I haven't guaranteed that T will implement the == operator. So I switched the code to this: if (myArgument.Equals(default(T))) Now this compiles, but will fail if myArgument is null, which is part of what I'm testing for. I can add an explicit null check like this: if (myArgument == null || myArgument.Equals(default(T))) Now this feels redundant to me. ReSharper is even suggesting that I change the myArgument == null part into myArgument == default(T) which is where I started. Is there a better way to solve this problem? I need to support both references types and value types. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
To avoid boxing, the best way to compare generics for equality is with EqualityComparer.Default. This respects IEquatable (without boxing) as well as object.Equals, and handles all the Nullable "lifted" nuances. Hence: if(EqualityComparer.Default.Equals(obj, default(T))) { return obj; } This will match: null for classes null (empty) for Nullable zero/false/etc for other structs

Related questions

0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    F# allows constraining generic types on the type's members, similar to: type ClassWithMemberConstraint = class end ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am using log4net in a C# project, in the production environment, I want to disable all the logging ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I am using log4net in a C# project, in the production environment, I want to disable all the logging ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Here is the example: if(value != ageValue) { ageValue = value; } I mean, if we assign the value ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    This is a Dropdown control where I am binding the data, after bind I am putting the select statement. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I want create expression with reflection in c#. I am target script is: using (var service = new ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    Hi Folks i was googling one more hours.i did not find anything relevant, I have text File like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
...