I use the "bool" type for variables as I was used to in C++, and I try to put the values of functions or properties I expect to be boolean into my variable. However I often encounter cases where the result type is "bool?" and not "bool" and the implicit casting fails.
我使用“bool”类型作为变量,就像我在C ++中习惯的那样,并且我尝试将我期望的函数或属性的值放入我的变量中。但是我经常遇到结果类型是“bool?”的情况。而不是“bool”,隐式转换失败。
What is the difference between the two and when is each used? Also, should I use "bool?" as the type for my variable? Is this the best practice?
两者之间有什么区别,每次使用的时间是多少?另外,我应该使用“bool?”作为我的变量的类型?这是最好的做法吗?
8 个解决方案
#1
The ?
symbol after a type is only a shortcut to the Nullable type, bool?
is equivalent to Nullable<bool>
.
的?一个类型后面的符号只是Nullable类型的快捷方式,bool?相当于Nullable
bool
is a value type, this means that it cannot be null
, so the Nullable type basically allows you to wrap value types, and being able to assign null
to them.
bool是一个值类型,这意味着它不能为null,因此Nullable类型基本上允许您包装值类型,并能够为它们赋值null。
bool?
can contain three different values: true
, false
and null
.
布尔?可以包含三个不同的值:true,false和null。
Also, there are no short-circuiting operators (&& ||) defined for bool?
此外,没有为bool定义的短路运算符(&& ||)?
Only the logical AND, inclusive OR, operators are defined and they behave like this:
只定义了逻辑AND,包含OR的运算符,它们的行为如下:
x y x & y x | y
true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null null
The Nullable type is basically a generic struct, that has the following public properties:
Nullable类型基本上是一个通用结构,具有以下公共属性:
public struct Nullable<T> where T: struct
{
public bool HasValue { get; }
public T Value { get; }
}
The HasValue
property indicates whether the current object has a value, and the Value
property will get the current value of the object, or if HasValue is false, it will throw an InvalidOperationException.
HasValue属性指示当前对象是否具有值,Value属性将获取对象的当前值,或者如果HasValue为false,则将抛出InvalidOperationException。
Now you must be wondering something, Nullable is a struct, a value type that cannot be null, so why the following statement is valid?
现在你必须想知道一些东西,Nullable是一个结构,一个不能为null的值类型,为什么以下语句有效?
int? a = null;
That example will compile into this:
那个例子将编译成这个:
.locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj valuetype [mscorlib]System.Nullable`1<int32>
A call to initobj, which initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type.
对initobj的调用,它将指定地址处的值类型的每个字段初始化为空引用或适当原始类型的0。
That's it, what's happening here is the default struct initialization.
就是这样,这里发生的是默认的struct初始化。
int? a = null;
Is equivalent to:
相当于:
Nullable<int> a = new Nullable<int>();
#2
bool?
is nullable while bool
is not.
布尔?可以为空,而布尔则不是。
bool? first;
bool second;
In the above code, first
will be null
while second
will be false
.
在上面的代码中,first将为null,而second将为false。
One typical use is if you want to know whether there has been an assignment made to the variable. Since bool
is a value type (just as int
, long
, double
, DateTime
and some other types), it will always be initialized to a default value (false
in the case of a bool
, 0
in the case of an int
). This means that you can not easily know whether it's false
because some code assigned false
to it, or if it is false
because it has not yet been assigned. In that case bool?
comes in handy.
一个典型的用法是,如果您想知道是否已对变量进行了赋值。由于bool是一个值类型(就像int,long,double,DateTime和其他一些类型),它将始终初始化为默认值(在bool的情况下为false,在int的情况下为0)。这意味着您不能轻易地知道它是否为假,因为某些代码对其赋予了错误,或者由于尚未分配,因此它是错误的。在那种情况下布尔?派上用场了。
#3
Whenever you see the ? character following a type name, it's shorthand for Nullable<TypeName>
. Nullable is a special type that allows value types to act like a null value. It's a way of explicitly expressing a value type can have a non-value value.
每当你看到了?类型名称后面的字符,它是Nullable
For bool it effectively turns the variable into a tri-state value
对于bool,它有效地将变量转换为三态值
- With Value: True
- With Value: False
- Without Value
有价值:真实
有价值:错误
#4
Adding ?
makes the type null-able. Which means you can do this:
添加?使类型为null。这意味着你可以这样做:
bool? x = null;
And it would be totally OK.
这将是完全可以的。
#5
bool? means the boolean is nullable and is syntactic sugar for a stucture Nullable<bool>
. Because a boolean is a value type, you cannot set it to null, but there are some cases where you'd want to like in a data access class because database fields can have null values.
布尔?意味着布尔值可以为空,并且是结构Nullable
#6
bool
can contain only true
and false
values while bool?
can also have a null
value.
布尔可以只包含真值和假值吗?也可以有一个空值。
#7
Another good place to use bool? is in a method to add null checking
另一个使用bool的好地方?是一个添加空检查的方法
public bool? IsTurkeyStillInFridge(Turkey turkey)
{
if (turkey == null)
return null;
else if (fridge.Contains(turkey))
return true;
else
return false;
}
bool? canStayAtDesk = IsTurkeyStillInFridge(turkey);
if (canStayAtDesk == null)
MessageBox.Show("No turkey this year, check for ham.");
else if (canStayAtDesk == true)
MessageBox.Show("Turkey for lunch. Stay at desk.");
else
MessageBox.Show("Turkey's gone, go out to lunch.");
#8
bool means you can have values of true and false. bool? means you can have a value of true, false, and null.
bool意味着你可以拥有真值和假值。布尔?意味着您可以具有true,false和null值。
It works for datetime and booleans.
它适用于日期时间和布尔值。
#1
The ?
symbol after a type is only a shortcut to the Nullable type, bool?
is equivalent to Nullable<bool>
.
的?一个类型后面的符号只是Nullable类型的快捷方式,bool?相当于Nullable
bool
is a value type, this means that it cannot be null
, so the Nullable type basically allows you to wrap value types, and being able to assign null
to them.
bool是一个值类型,这意味着它不能为null,因此Nullable类型基本上允许您包装值类型,并能够为它们赋值null。
bool?
can contain three different values: true
, false
and null
.
布尔?可以包含三个不同的值:true,false和null。
Also, there are no short-circuiting operators (&& ||) defined for bool?
此外,没有为bool定义的短路运算符(&& ||)?
Only the logical AND, inclusive OR, operators are defined and they behave like this:
只定义了逻辑AND,包含OR的运算符,它们的行为如下:
x y x & y x | y
true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null null
The Nullable type is basically a generic struct, that has the following public properties:
Nullable类型基本上是一个通用结构,具有以下公共属性:
public struct Nullable<T> where T: struct
{
public bool HasValue { get; }
public T Value { get; }
}
The HasValue
property indicates whether the current object has a value, and the Value
property will get the current value of the object, or if HasValue is false, it will throw an InvalidOperationException.
HasValue属性指示当前对象是否具有值,Value属性将获取对象的当前值,或者如果HasValue为false,则将抛出InvalidOperationException。
Now you must be wondering something, Nullable is a struct, a value type that cannot be null, so why the following statement is valid?
现在你必须想知道一些东西,Nullable是一个结构,一个不能为null的值类型,为什么以下语句有效?
int? a = null;
That example will compile into this:
那个例子将编译成这个:
.locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj valuetype [mscorlib]System.Nullable`1<int32>
A call to initobj, which initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type.
对initobj的调用,它将指定地址处的值类型的每个字段初始化为空引用或适当原始类型的0。
That's it, what's happening here is the default struct initialization.
就是这样,这里发生的是默认的struct初始化。
int? a = null;
Is equivalent to:
相当于:
Nullable<int> a = new Nullable<int>();
#2
bool?
is nullable while bool
is not.
布尔?可以为空,而布尔则不是。
bool? first;
bool second;
In the above code, first
will be null
while second
will be false
.
在上面的代码中,first将为null,而second将为false。
One typical use is if you want to know whether there has been an assignment made to the variable. Since bool
is a value type (just as int
, long
, double
, DateTime
and some other types), it will always be initialized to a default value (false
in the case of a bool
, 0
in the case of an int
). This means that you can not easily know whether it's false
because some code assigned false
to it, or if it is false
because it has not yet been assigned. In that case bool?
comes in handy.
一个典型的用法是,如果您想知道是否已对变量进行了赋值。由于bool是一个值类型(就像int,long,double,DateTime和其他一些类型),它将始终初始化为默认值(在bool的情况下为false,在int的情况下为0)。这意味着您不能轻易地知道它是否为假,因为某些代码对其赋予了错误,或者由于尚未分配,因此它是错误的。在那种情况下布尔?派上用场了。
#3
Whenever you see the ? character following a type name, it's shorthand for Nullable<TypeName>
. Nullable is a special type that allows value types to act like a null value. It's a way of explicitly expressing a value type can have a non-value value.
每当你看到了?类型名称后面的字符,它是Nullable
For bool it effectively turns the variable into a tri-state value
对于bool,它有效地将变量转换为三态值
- With Value: True
- With Value: False
- Without Value
有价值:真实
有价值:错误
#4
Adding ?
makes the type null-able. Which means you can do this:
添加?使类型为null。这意味着你可以这样做:
bool? x = null;
And it would be totally OK.
这将是完全可以的。
#5
bool? means the boolean is nullable and is syntactic sugar for a stucture Nullable<bool>
. Because a boolean is a value type, you cannot set it to null, but there are some cases where you'd want to like in a data access class because database fields can have null values.
布尔?意味着布尔值可以为空,并且是结构Nullable
#6
bool
can contain only true
and false
values while bool?
can also have a null
value.
布尔可以只包含真值和假值吗?也可以有一个空值。
#7
Another good place to use bool? is in a method to add null checking
另一个使用bool的好地方?是一个添加空检查的方法
public bool? IsTurkeyStillInFridge(Turkey turkey)
{
if (turkey == null)
return null;
else if (fridge.Contains(turkey))
return true;
else
return false;
}
bool? canStayAtDesk = IsTurkeyStillInFridge(turkey);
if (canStayAtDesk == null)
MessageBox.Show("No turkey this year, check for ham.");
else if (canStayAtDesk == true)
MessageBox.Show("Turkey for lunch. Stay at desk.");
else
MessageBox.Show("Turkey's gone, go out to lunch.");
#8
bool means you can have values of true and false. bool? means you can have a value of true, false, and null.
bool意味着你可以拥有真值和假值。布尔?意味着您可以具有true,false和null值。
It works for datetime and booleans.
它适用于日期时间和布尔值。