I use intas an example, but this applies to any type of value in .Net
In .Net 1, the following would eliminate the compiler exception:
int i = SomeFunctionThatReturnsInt();
if( i == null )
Now (in .Net 2 or 3.5) this exception has passed.
I know why this is:
int? j = null;
if( i == j )
The problem is that since int?it is NULL, it intnow has an implicit cast to int?. The syntax above is the compiler magic. In fact, we do:
Nullable<int> j = null;
if( (Nullable<int>) i == j)
if( i == (int) j)
So, when we do i == null, we get:
if( (Nullable<int>) i == null )
Given that C # does the compiler logic to calculate this anyway, why can't it be smart enough not to do this when dealing with absolute values like null?