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

前端 未结 8 1386
迷失自我
迷失自我 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:11

    In C# using an uninitialized variable is not allowed.

    So

    int i;
    Console.Writeline(i);
    

    Results in a compilation error.

    You can initialize int with new such as:

    int anInt = new int();
    

    This will result in the Default value for int which is 0. In cases where you do wish to have a generic int one can make the int nullable with the syntax

    int? nullableInt = null;
    

提交回复
热议问题