问题描述
int value=0; if (value == 0) { value = null; }
如何将上面的 value 设置为 null?
How can I set value to null above?
任何帮助将不胜感激.
推荐答案
在 .Net 中,您不能将 null 值分配给 int 或任何其他结构.相反,使用 Nullable
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; }
进一步阅读
- 可空类型(C# 编程指南)