繁体   English   中英

将整数声明为null

[英]Declare integer as null

通常,我们像这样对变量属性进行delare:

int a = 0;

我想将一个整数声明为null 我怎样才能做到这一点?

我的预期输出是

int i = null;

您可以使用Nullable<T>类型:

int? i = null;

C#数据类型分为值类型和引用类型。 默认情况下,值类型不能为空。 但对于引用类型,则为null。

string name = null;
Int ? i = null; // declaring nullable type

如果要使值类型可为空,请使用?

Int j = i;  //this will through the error because implicit conversion of nullable 
            // to non nullable is not possible `

采用

int j =i.value;

要么

int j =(int) i;

除非您明确定义c#中的值类型,否则它们不能为空。 如果要允许int为空,则必须这样声明变量:

int? i = null;

整数是一种值类型,其默认值在初始化时为0。

https://msdn.microsoft.com/zh-CN/library/83fhsxwc.aspx

您只是不能将其设置为null,并且编译器将不允许您使用未初始化的整数。

如果出于任何原因要求将null分配给整数,则应使用引用类型Nullable。 诠释? = null。 我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM