問題描述
表達(dá)式 (n == null
) 的結(jié)果是否總是 false
因?yàn)?br> int
類型的值永遠(yuǎn)不等于 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 表達(dá)式的結(jié)果始終為false",因?yàn)閕nt"類型的值永遠(yuǎn)不會(huì)等于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?'
推薦答案
如果您希望整數(shù)變量允許空值,請(qǐng)將其聲明為 可空類型:
If you want your integer variable to allow null values, declare it to be a nullable type:
注意 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.
這篇關(guān)于為什么輸入“int"永遠(yuǎn)不等于“空"?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!