Consider the following code:
DateTime t = DateTime.Today; bool isGreater = t > null;
With Visual Studio 2010 (C # 4, .NET 4.0), I get the following warning:
warning CS0458: The result of the expression is always "null" of type "bool?"
This is not true; the result is always false (like bool ):
Now the DateTime structure overloads the > (more) operator. Any invalid structure (e.g. DateTime) is implicitly converted to the corresponding type Nullable<> . The above expression is exactly equivalent
bool isGreater = (DateTime?)t > (DateTime?)null;
which also generates the same wrong warning. Here, the > operator is cleared . This works by returning false if HasValue either of the two operands is false . Otherwise, the operator raised will go to expand the two operands to the base structure, and then cause the overload > defined by this structure (but this is not necessary in this case, when one operand does not have HasValue ).
Can you reproduce this error and this error is known? I didnโt understand something?
This is the same for all types of structures (not simple int types, not enumeration types) that overload the corresponding operator.
(Now, if we use == instead of > , everything should be completely similar (because DateTime also overloads the == operator), but that doesn't look like it. If I say
DateTime t = DateTime.Today; bool isEqual = t == null;
I get a warning no โน Sometimes you see that people randomly check a variable or parameter for null, not realizing that the type of their variable is a structure (which overloads == and which is not a simple type of type int ). It would be better if they received a warning.)
Update: Using the Visual Studio 2015 C # 6.0 compiler (based on Roslyn ), the invalid message from isGreater above is changed to CS0464 with the correct and useful warning message. In addition, the lack of warning with isEqual above is fixed in the VS2015 compiler, but only if you compile with /features:strict .