如何在c#中将空值设置为int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19522225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set null value to int in c#?
提问by user2902180
int value=0;
if (value == 0)
{
value = null;
}
How can I set value
to null
above?
我如何设置value
为null
上面?
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by p.s.w.g
In .Net, you cannot assign a null
value to an int
or any other struct. Instead, use a Nullable<int>
, or int?
for short:
在 .Net 中,您不能null
为 anint
或任何其他结构赋值。相反,使用 a Nullable<int>
,或int?
简称:
int? value = 0;
if (value == 0)
{
value = null;
}
Further Reading
进一步阅读
回答by Jon
You cannot set an int
to null
. Use a nullable int (int?
) instead:
您不能将 设置int
为null
。使用可为空的 int ( int?
) 代替:
int? value = null;
回答by Francis Tchatchoua
Declare you integer variable as nullable
eg: int? variable=0; variable=null;
将整数变量声明为可为空的,例如: int? variable=0; variable=null;
回答by doublehelix
Additionally, you cannot use "null" as a value in a conditional assignment. e.g...
此外,您不能在条件赋值中使用“null”作为值。例如..
bool testvalue = false;
int? myint = (testvalue == true) ? 1234 : null;
FAILS with: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '<null>'.
失败: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '<null>'.
So, you have to cast the null as well... This works:
所以,你也必须投射空值......这有效:
int? myint = (testvalue == true) ? 1234 : (int?)null;
回答by El Belo
Use Null.NullInteger ex: private int _ReservationID = Null.NullInteger;
使用 Null.NullInteger 例如:private int _ReservationID = Null.NullInteger;
回答by A J
int does not allow null, use-
int 不允许为空,请使用-
int? value = 0
or use
或使用
Nullable<int> value
回答by Amadeu Antunes
public static int? Timesaday { get; set; } = null;
OR
或者
public static Nullable<int> Timesaday { get; set; }
or
或者
public static int? Timesaday = null;
or
或者
public static int? Timesaday
or just
要不就
public static int? Timesaday { get; set; }
static void Main(string[] args)
{
Console.WriteLine(Timesaday == null);
//you also can check using
Console.WriteLine(Timesaday.HasValue);
Console.ReadKey();
}
The null keyword is a literal that represents a null reference, one that does not refer to any object. In programming, nullable types are a feature of the type system of some programming languages which allow the value to be set to the special value NULL instead of the usual possible values of the data type.
null 关键字是表示空引用的文字,不引用任何对象。在编程中,可空类型是某些编程语言的类型系统的一个特性,它允许将值设置为特殊值 NULL,而不是数据类型的通常可能值。
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nullhttps://en.wikipedia.org/wiki/Null
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null https://en.wikipedia.org/wiki/Null
回答by dreamboatDev
int ? index = null;
public int Index
{
get
{
if (index.HasValue) // Check for value
return index.Value; //Return value if index is not "null"
else return 777; // If value is "null" return 777 or any other value
}
set { index = value; }
}