I have a few places where I have a generic type parameter that is not limited to class (or struct), and when I try to compare variables of that type against null, Resharper underlines it, complaining that I may be comparing a value type to null (a valid objection, to be sure). Is there an accepted way of checking if a variable is a value type before comparing against null?
For example:
public TObject MyProperty { get; set; }
...
private void SomeMethod()
{
if(MyProperty == null) //Warning here
{
...
}
}
I’ve been doing if(!(MyProperty is ValueType) && MyProperty)–is that valid? It doesn’t get rid of the warning, but that doesn’t necessarily mean anything.
What do you want to do in the case that it is a value type? sometimes I will do:
Or:
See here.