What#39;s the difference between quot;boolquot; and quot;bool?quot;?(“bool和“bool有什么区别?和“布尔??)
问题描述
我对变量使用bool"类型,就像我在 C++ 中习惯的那样,我尝试将我希望为布尔值的函数或属性的值放入我的变量中.但是我经常遇到结果类型为bool"的情况.而不是布尔",隐式转换失败.
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"吗?作为我的变量的类型?这是最佳做法吗?
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?
等价于可空
The ?
symbol after a type is only a shortcut to the Nullable type, bool?
is equivalent to Nullable<bool>
.
bool
是 值类型, 这意味着它不能是 null
,所以 Nullable 类型基本上允许你包装值类型,并且能够将 null
分配给它们.
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?
可以包含三个不同的值:true
、false
和 null
.
bool?
can contain three different values: true
, false
and null
.
此外,没有为 bool?
仅定义了逻辑 AND(包括 OR)运算符,它们的行为如下:
Only the logical AND, inclusive OR, operators are defined and they behave like this:
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
Nullable 类型基本上是一个泛型结构,具有以下公共属性:
The Nullable type is basically a generic struct, that has the following public properties:
public struct Nullable<T> where T: struct
{
public bool HasValue { get; }
public T Value { get; }
}
HasValue
属性表示当前对象是否有值,Value
属性会获取对象的当前值,如果HasValue为false则抛出 InvalidOperationException.
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.
现在你一定很疑惑,Nullable是一个struct,一个不能为null的值类型,那么为什么下面的语句是有效的呢?
Now you must be wondering something, Nullable is a struct, a value type that cannot be null, so why the following statement is valid?
int? a = null;
该示例将编译为:
.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>
调用 initobj,它将指定地址处的值类型的每个字段初始化为空引用或相应原始类型的 0.
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.
就是这样,这里发生的是默认的 struct初始化.
That's it, what's happening here is the default struct initialization.
int? a = null;
相当于:
Nullable<int> a = new Nullable<int>();
这篇关于“bool"和“bool"有什么区别?和“布尔?"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“bool"和“bool"有什么区别?和“布尔?"?


- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01