Is there any background difference between two declarations:
var x = (string)null;
and
string x = null;
Will the runtime treat this declarations different ways? Will the compiler produce the same IL?
Is there any background difference between two declarations:
var x = (string)null;
and
string x = null;
Will the runtime treat this declarations different ways? Will the compiler produce the same IL?
 
    
     
    
    Yes, it produces the same IL:
void Main()
{
    var x = (string)null;
    string y = null;
}
Produces (with optimizations turned off):
IL_0000:  nop         
IL_0001:  ldnull      
IL_0002:  stloc.0     // x
IL_0003:  ldnull      
IL_0004:  stloc.1     // y
IL_0005:  ret        
From the compilers perspective, you are assigning null to a string variable.
 
    
    In the first case the compiler does not know the type of x unless you specify it in the cast. The resulting IL codes are however the same in both cases.
