Generic type comparison with null

I'm writing a generic class in which I need to compare the generic type
member with null and used this code:

public class Blah]]>
{
public void GenericNullTest(T item)
{
if(item == null)
{
throw new ArgumentNullException("item");
}
else
{
Console.WriteLine("item is not null");
}
}
}

ReSharper highlights the statement with the message "Possible compare of
value type with 'null'" but I think the code is actually correct because the
JIT compiler effectively nops out the comparison if T is a non-nullable
value type. You can kinda sorta confirm this by stepping through the code
when T as an int and it works fine.

Please sign in to leave a comment.