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

旧城冷巷雨未停 提交于 2019-12-17 16:13:56

问题


int n == 0;

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

Is it true that the result of expression (n == null) is always false since
a   value of type int  is never equal to   null of type int? (see warning below)

Warning CS0472 The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'


回答1:


If you want your integer variable to allow null values, declare it to be a nullable type:

int? n = 0;

Note the ? after int, which means that type can have the value null. Nullable types were introduced with v2.0 of the .NET Framework.




回答2:


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;



回答3:


Because int is a value type rather than a reference type. The C# Language Specification doesn't allow an int to contain null. Try compiling this statement:

int x = null ;

and see what you get.

You get the compiler warning because it's a pointless test and the compiler knows it.




回答4:


"Value types" in .NET (like int, double, and bool) cannot, by definition, be null - they always have an actual value assigned. Check out this good intro to value types vs. reference types.




回答5:


The usage of NULL applies to Pointers and References in general. A value 0 assigned to an integer is not null. However if you can assign a pointer to the integer and assign it to NULL, the statement is valid.

To sum up =>

 /*Use the keyword 'null' while assigning it to pointers and references. Use 0 for integers.*/



回答6:


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.




回答7:


No, because int is a value type. int is a Value type like Date, double, etc. So there is no way to assigned a null value.




回答8:


 public static int? n { get; set; } = null;

OR

 public static Nullable<int> n { get; set; }

or

 public static int? n = null;

or

 public static int? n

or just

 public static int? n { get; set; } 


    static void Main(string[] args)
    {


    Console.WriteLine(n == null);

     //you also can check using 
     Console.WriteLine(n.HasValue);

        Console.ReadKey();
    }

The null keyword is a literal that represents a null reference, one that does not refer to any object. In programming, nullable types are a feature of the type system of some programming languages which allow the value to be set to the special value NULL instead of the usual possible values of the data type.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null https://en.wikipedia.org/wiki/Null



来源:https://stackoverflow.com/questions/6191339/why-type-int-is-never-equal-to-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!