且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#中将null值设置为int?

更新时间:2023-01-31 15:48:30

在.Net中,您不能将 null 值分配给 int 或任何其他结构。相反,请使用 Nullable< int> int?简称:

In .Net, you cannot assign a null value to an int or any other struct. Instead, use a Nullable<int>, or int? for short:

int? value = 0;

if (value == 0)
{
    value = null;
}

进一步阅读

  • Nullable Types (C# Programming Guide)