什么是布尔?返回类型是什么意思? [英] What does the bool? return type mean?
问题描述
我看到一个方法的返回布尔?
,没有人知道这是什么意思?
I saw a method return bool?
, does anyone know the meaning of it?
推荐答案
T
是一个C#语法快捷方式可空< T>
。因此,布尔?
映射到 可空<布尔>
。因此,在你的情况下,它是一个结构,要么是空
,或有一个布尔
值。
T?
is a C# syntax shortcut to Nullable<T>
. So bool?
maps to Nullable<bool>
. So in your case it's a struct that either is null
, or has a bool
value.
如果一个可空&LT; T&GT;
是空
这是一个有点从引用类型为空值不同。它基本上是一个包含基本类型和一个布尔标志 hasValue的
A结构。但运行时和C#语言有一点神奇pretend一个可空
与 hasValue的==假
真是空。但不同的有时仍然会泄漏。
If a Nullable<T>
is null
that's a bit different from a reference type being null. It basically is a struct containing the underlying type and a boolean flag HasValue
. But both the runtime and the C# language have a bit of magic to pretend that a Nullable
with HasValue==false
is really null. But the difference still leaks sometimes.
的基本类型是隐式转换为可空类型(布尔
- > 布尔
?)。您可以通过可空类型的基础类型,你可以明确地投((布尔)myNullableBool
或值
物业( myNullableBool.Value
)。您应该检查空
虽然之前,否则你会得到一个异常,如果可为空值空
。
The underlying type is implicitly convertible to the nullable type (bool
->bool?
). To get the underlying type from the nullable type, you can either cast explicitly ((bool)myNullableBool
or use the Value
property (myNullableBool.Value
). You should check for null
before though, or you'll get an exception if the nullable value is null
.
- 可空类型(C#编程指南) 李>
- <一个href="http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx"><$c$c>Nullable<bool>$c$c>
这篇关于什么是布尔?返回类型是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!