c# - .Net 2+: why does if( 1 == null ) no longer throw a compiler exception? -
i'm using int
example, applies value type in .net
in .net 1 following throw compiler exception:
int = somefunctionthatreturnsint(); if( == null ) //compiler exception here
now (in .net 2 or 3.5) exception has gone.
i know why is:
int? j = null; //nullable int if( == j ) //this shouldn't throw exception
the problem because int?
nullable , int
has implicit cast int?
. syntax above compiler magic. we're doing:
nullable<int> j = null; //nullable int //compiler smart enough if( (nullable<int>) == j) //and not if( == (int) j)
so now, when i == null
get:
if( (nullable<int>) == null )
given c# doing compiler logic calculate anyway why can't smart enough not when dealing absolute values null
?
i don't think compiler problem per se; integer value never null, idea of equating them isn't invalid; it's valid function returns false. , compiler knows; code
bool oneisnull = 1 == null;
compiles, gives compiler warning: the result of expression 'false' since value of type 'int' never equal 'null' of type '<null>'
.
so if want compiler error back, go project properties , turn on 'treat warnings errors' error, , you'll start seeing them build-breaking problems again.
Comments
Post a Comment