使用“?”铸造期间

发布于 2025-01-05 04:41:45 字数 338 浏览 2 评论 0原文

我有一段示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

记忆消瘦 2025-01-12 04:41:45

bool? 类型是 Nullable 的简写。

该代码不能按原样编译。您将收到错误消息“无法隐式转换类型‘bool?’” '布尔'。”。

如果您也将变量声明为可为空,它将起作用:

bool? _HasParsed;

这当然可能意味着您需要在代码中进行其他更改。您可以使用 _HasParsed.HasValue 检查变量是否不为 null,并使用 _HasParsed.Value 获取 bool 值。

The type bool? is shorthand for Nullable<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:

bool? _HasParsed;

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.

自控 2025-01-12 04:41:45

bool?Nullable 的简写。但您的代码不需要它,因为在赋值之前会检查 null

已编辑:正如 Jeow Li Huan 在评论中正确指出的那样,对 null 的检查意味着 GetCheckedStatus() 的返回值的类型为 <代码>布尔?。以下简化的片段将与OP问题中的原始代码执行相同的操作:

object IsCheckedAsObj = GetCheckedStatus();
if (IsCheckedAsObj == null)
    throw new InvalidOperationException("Status not found");
bool _HasParsed = (bool)IsCheckedAsObj;

bool? is shorthand for Nullable<bool>. Your code doesn't need it though, because there's a check against null before the assignment.

Edited: As Jeow Li Huan correctly stated in the comments, the check for null implies that the return value of GetCheckedStatus() is of type bool?. The following simplified snipped will do the same as the original code in OP's question:

object IsCheckedAsObj = GetCheckedStatus();
if (IsCheckedAsObj == null)
    throw new InvalidOperationException("Status not found");
bool _HasParsed = (bool)IsCheckedAsObj;
焚却相思 2025-01-12 04:41:45

boolbool? 之间的区别在于 bool? 是一个 Nullable,这意味着 bool具有三个可能的值:falsetruenull(= 未设置)。

bool? nullableBool = null;

if ( b.HasValue )
{
   bool notNullableBool = b.Value;
}

此处是 MSDN 文档。

The difference between bool and bool? is that bool? is a Nullable<bool> which means that the bool has three possible values: false, true and null (= not set).

bool? nullableBool = null;

if ( b.HasValue )
{
   bool notNullableBool = b.Value;
}

Here is the MSDN documentation.

懒的傷心 2025-01-12 04:41:45

带有 ? 的类型,例如 bool?可为空。了解如何使用它们

它还可以表示为 Nullable。并且只能应用于 struct 类型。

可空类型的特点是您可以将其值或 null 分配给它们。

使用:

int? number;
// do something with number
if (number.HasValue)
{
  Console.WriteLine(number.Value);
}
else
{
  Console.WriteLine("No value");
}

Console.WriteLine(number.HasValue ? number.Value : "No value");

Types with ?, such as bool? are nullable. Learn how to use them.

It also can be represented as Nullable<bool>. And can only be applied for struct types.

Nullable type's feature is that you can assign its value or null to them.

Using:

int? number;
// do something with number
if (number.HasValue)
{
  Console.WriteLine(number.Value);
}
else
{
  Console.WriteLine("No value");
}

or

Console.WriteLine(number.HasValue ? number.Value : "No value");
醉殇 2025-01-12 04:41:45

这意味着它是一个 boolean,也可以是 null

请参阅 MSDN 上的可空类型

It means it's a boolean that can also be null.

See Nullable Types on MSDN.

蓝礼 2025-01-12 04:41:45

? 是以下结构的简写:

struct Nullable<T> 
{ 
    public bool HasValue; 
    public T Value; 
} 

您可以直接使用该结构,但是 ?是使生成的代码更加简洁的快捷语法。而不是输入:
可空 x = 新的可空(125);

相反,你可以写:
整数? x = 125;

这不适用于字符串,因为字符串是引用类型而不是值类型。

The ? is shorthand for the struct below:

struct Nullable<T> 
{ 
    public bool HasValue; 
    public T Value; 
} 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文