什么是布尔?返回类型是什么意思? [英] What does the bool? return type mean?

查看:325
本文介绍了什么是布尔?返回类型是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个方法的返回布尔?,没有人知道这是什么意思?

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; 这是一个有点从引用类型为空值不同。它基本上是一个包含基本类型和一个布尔标志 hasVa​​lue的 A结构。但运行时和C#语言有一点神奇pretend一个可空 hasVa​​lue的==假真是空。但不同的有时仍然会泄漏。

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.

这篇关于什么是布尔?返回类型是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆