問題描述
int n == 0;
if (n == null)
{
Console.WriteLine("......");
}
表達式 (n == null
) 的結果是否總是 false
因為
int
類型的值永遠不等于 null
類型 int?
(見下面的警告)
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)
警告 CS0472 表達式的結果始終為false",因為int"類型的值永遠不會等于int"類型的null"?
Warning CS0472 The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'
推薦答案
如果您希望整數變量允許空值,請將其聲明為 可空類型:
If you want your integer variable to allow null values, declare it to be a nullable type:
int? n = 0;
注意 int 之后的 ?
,這意味著該類型可以具有值 null
..NET Framework v2.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.
這篇關于為什么輸入“int"永遠不等于“空"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!