为什么类型" INT"永远不会等于“空”? [英] Why type "int" is never equal to 'null'?

查看:132
本文介绍了为什么类型" INT"永远不会等于“空”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int n == 0;

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

这是真的,当然pression的结果(ñ== 0 )始终是假的,因为类型的值 INT 永远等于 INT?

Is it true that the result of expression (n == 0) is always false since a value of type int never equals 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之后,这意味着该类型的值可以是。可空类型进行了介绍与.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"永远不会等于“空”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆