通常,我们像这样选择变量属性:
int a = 0;
我想将一个整数声明为 null。我该怎么做?
我的预期输出是
int i = null;
Nullable<T>
类型:
int? i = null;
C# 数据类型分为值类型和引用类型。默认情况下 值类型不可为空。但对于引用类型为空。
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 的 null,你必须像这样声明你的变量:
int? i = null;
整数是一种值类型,初始化时的默认值为 0。
https://msdn.microsoft.com/en-us/library/83fhsxwc.aspx
您只是不能将其设为 null,并且编译器不会允许您使用未初始化的整数。
如果需要将 null 分配给整数,无论出于何种原因,都应使用引用类型 Nullable。 int? = null 。我希望这有所帮助。