Why type “int” is never equal to 'null'?

前端 未结 8 1379
迷失自我
迷失自我 2020-12-05 22:50
int n == 0;

if (n == null)    
{  
    Console.WriteLine(\"......\");  
}

Is it true that the result of expression (n == null) is alw

8条回答
  •  一向
    一向 (楼主)
    2020-12-05 23:12

    Very simply put, an int is a very basic item. It's small and simple so that it can be handled quickly. It's handled as the value directly, not along the object/pointer model. As such, there's no legal "NULL" value for it to have. It simply contains what it contains. 0 means a 0. Unlike a pointer, where it being 0 would be NULL. An object storing a 0 would have a non-zero pointer still.

    If you get the chance, take the time to do some old-school C or assembly work, it'll become much clearer.

提交回复
热议问题