使用“?”铸造期间
我有一段示例代码:
bool _HasParsed;
object IsCheckedAsObj = GetCheckedStatus();
if (IsCheckedAsObj == null)
{
throw new InvalidOperationException("Status not found");
}
_HasParsed = (bool?)IsCheckedAsObj; //why (bool?) instead of (bool)
在最后一行,我可以理解他们正在将对象解析为布尔值。但那是什么?在那里做什么? (bool?) 和 (bool) 有什么区别?
I have a sample piece of code with me:
bool _HasParsed;
object IsCheckedAsObj = GetCheckedStatus();
if (IsCheckedAsObj == null)
{
throw new InvalidOperationException("Status not found");
}
_HasParsed = (bool?)IsCheckedAsObj; //why (bool?) instead of (bool)
In the last line, I can understand that they are parsing the object to boolean. But what is that '?' doing there? Whats the difference between (bool?) instead of (bool)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
bool?
类型是Nullable
的简写。该代码不能按原样编译。您将收到错误消息“无法隐式转换类型‘bool?’” '布尔'。”。
如果您也将变量声明为可为空,它将起作用:
这当然可能意味着您需要在代码中进行其他更改。您可以使用
_HasParsed.HasValue
检查变量是否不为 null,并使用_HasParsed.Value
获取 bool 值。The type
bool?
is shorthand forNullable<bool>
.The code doesn't compile as it stands. You will get the error message "Cannot implicitly convert type 'bool?' to 'bool'.".
If you declare the variable as nullable too, it will work:
That might of course mean that you need to do other changes in the code. You can use
_HasParsed.HasValue
to check if the variable is not null, and use_HasParsed.Value
to get the bool value.bool?
是Nullable
的简写。但您的代码不需要它,因为在赋值之前会检查null
。已编辑:正如 Jeow Li Huan 在评论中正确指出的那样,对
null
的检查意味着GetCheckedStatus()
的返回值的类型为 <代码>布尔?。以下简化的片段将与OP问题中的原始代码执行相同的操作:bool?
is shorthand forNullable<bool>
. Your code doesn't need it though, because there's a check againstnull
before the assignment.Edited: As Jeow Li Huan correctly stated in the comments, the check for
null
implies that the return value ofGetCheckedStatus()
is of typebool?
. The following simplified snipped will do the same as the original code in OP's question:bool
和bool?
之间的区别在于bool?
是一个Nullable
,这意味着 bool具有三个可能的值:false
、true
和null
(= 未设置)。此处是 MSDN 文档。
The difference between
bool
andbool?
is thatbool?
is aNullable<bool>
which means that the bool has three possible values:false
,true
andnull
(= not set).Here is the MSDN documentation.
带有
?
的类型,例如bool?
是 可为空。了解如何使用它们。它还可以表示为
Nullable
。并且只能应用于 struct 类型。可空类型的特点是您可以将其值或
null
分配给它们。使用:
或
Types with
?
, such asbool?
are nullable. Learn how to use them.It also can be represented as
Nullable<bool>
. And can only be applied forstruct
types.Nullable type's feature is that you can assign its value or
null
to them.Using:
or
这意味着它是一个
boolean
,也可以是null
。请参阅 MSDN 上的可空类型。
It means it's a
boolean
that can also benull
.See Nullable Types on MSDN.
?
是以下结构的简写:您可以直接使用该结构,但是 ?是使生成的代码更加简洁的快捷语法。而不是输入:
可空 x = 新的可空(125);
相反,你可以写:
整数? x = 125;
这不适用于字符串,因为字符串是引用类型而不是值类型。
The
?
is shorthand for the struct below:You can use this struct directly, but the ? is the shortcut syntax to make the resulting code much cleaner. Rather than typing:
Nullable x = new Nullable(125);
Instead, you can write:
int? x = 125;
This doesn't work with string, as a string is a Reference type and not a Value type.