- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我在 Microsoft MSDN 上找到这篇文章时,我正在研究可为空的 boolean 值
How to: Identify a Nullable Type (C# Programming Guide)
You can use the C# typeof operator to create a Type object that represents a Nullable type.
所以我尝试使用可为 null 的 boolean 值进行检查:
Console.Write(typeof(bool?)); //System.Nullable`1[System.Boolean]
MSDN 上的文章说
You can also use the classes and methods of the System.Reflection namespace to generate Type objects that represent Nullable types. However, if you try to obtain type information from Nullable variables at runtime by using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself.
Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.
如果这是真的,我希望从 .GetType()
得到相同的结果,无论我使用可为空的 bool 还是常规 bool。但事实并非如此:
bool a = new bool();
Console.Write(a.GetType()); //Prints System.Boolean
bool? b = new bool?();
Console.Write(b.GetType()); //Exception!
发生的异常:
An unhandled exception of type 'System.NullReferenceException' occurred in BoolTest.exe
Additional information: Object reference not set to an instance of an object.
但是对象引用被设置为一个对象的实例。导致此错误的原因可能是什么?
最佳答案
您的实际问题似乎是:
By initializing a
Nullable<T>
to its default value using its default constructor, for examplebool? b = new bool?();
, why does accessingb
's members such asGetType()
throw aNullReferenceException
? Doesn'tnew
always return a value, sob
can't really benull
?
好吧,是的,不是。 Nullable<T>
有点特别。来自 C# 规范:
4.1.10 Nullable types
[...]
An instance of a nullable type T? has two public read-only properties:
- A HasValue property of type bool
- A Value property of type T
An instance for which HasValue is true is said to be non-null. A non-null instance contains a known value and Value returns that value. An instance for which HasValue is false is said to be null. A null instance has an undefined value. Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown.
所以是的,bool? b = new bool?();
确实返回一个实例:一个你只能调用HasValue
的实例在。因为它返回 false
,您无法对该实例做很多其他事情。
然后是下一个相关部分:
4.3.1 Boxing conversions
Boxing a value of a nullable-type produces a null reference if it is the null value (HasValue is false)
MSDN: Boxing Nullable Types (C# Programming Guide) 中也对此进行了解释:
Objects based on nullable types are only boxed if the object is non-null. If HasValue is false, the object reference is assigned to null instead of boxing.
进一步了解规范:
11.3.5 Boxing and unboxing
When a struct type overrides a virtual method inherited from System.Object (such as Equals, GetHashCode, or ToString), invocation of the virtual method through an instance of the struct type does not cause boxing to occur.
GetType()
未被 Nullable<T>
覆盖,所以会发生拳击。当您调用 GetType()
或结构上的任何非重写方法,该结构将在调用该方法之前被装箱到一个对象。在空的情况下 Nullable<T>
, 该装箱操作的结果将是 (object)null
.因此异常(exception)。
另见 Does calling a method on a value type result in boxing in .NET? .
所以,回答你的问题:
b
是不 null,它包含一个Nullable<bool>
用HasValue
指示false
.GetType()
方法,导致 Nullable<bool>
结构被装箱以访问底层方法 object.GetType()
.(object)null
。 .((object)null).GetType()
,抛出 NullReferenceException
你遇到了。如果您实际上正在寻找一段可以返回任何变量类型的代码,即使是 null Nullable<T>
,使用这样的东西:
Type GetType<T>(T obj)
{
return typeof(T);
}
然后你可以这样调用:
Console.WriteLine(GetType(b));
关于c# - 可空 boolean 值上的 GetType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551698/
我正在尝试从文本文件构建 boolean 值[][]。我正在读取每个字符,存储在 ArrayList 中(1 为真,0 为假)。我试过 ArrayList,但出现编译错误,意外元素。因此,我构建了一个
我想知道编程语言中的运算符优先级是否取决于实现,还是所有语言都遵循固定的规则。并且,如果可能的话,您可以先排序以下具有最高优先级的运算符吗:AND,OR,NOT,XOR。 最佳答案 我在Google上
这是同样的事情,对吗?还是有细微的差别?我只是想确保我没有误解任何事情。 最佳答案 通过简单地将 AND 替换为 OR 以及将 OR 替换为 AND 即可生成 boolean 对偶。补码本身不受影响,
我想这对于大多数优秀的程序员来说是微不足道的,但我已经习惯使用 true 进行编程。和 false 2、当我遇到0和1的时候,我永远记不住哪一个是真的,哪一个是假的。 有什么建议? 1好:I mean
我正在尝试将此 Java 示例转换为 Kotlin: Gson gson = new GsonBuilder() .registerTypeAdapter(Boolean.class,
下面的代码打印 true。 public static void main(String[] args) { Boolean test = false; test =
我在处理应该导致在 iReport 中显示或隐藏 strip 的表达式时遇到困难。 这些是我拥有的变量: Integer mainGroupInt = Integer.valueOf(5); Inte
以下编码错误可能是因为 Boolean equals(Object) 方法不需要 boolean/Boolean 参数: private void foo() { Boolean isSome
我想简化一个 boolean 表达式。 表达式是这样的 X1 xor (X2 || X3 && X4 || x5) 如何使用 boolean 代数规则简化此表达式。 而且我想将上面的 boolean
我正在使用一些工具,它可以确定特定事务是否成功的唯一方法是它是否通过了各种检查。但是,它的方式有限制,一次只能做一次检查,而且必须是顺序的。一切都必须从左到右计算。 例如, A || C && D 它
在大多数编程语言中,1和 0可以用来代替 True和 False .然而,根据我的经验,整数似乎总是更容易使用。 以下是我的意思的一些示例: if x is True: x = False else:
我有一个 boolean 方程,想简化它。帮忙解决一下。 bool needLoad = isA || (!isA && !isB); 之后我使用 if (needLoad){ if (
我认为这始终是正确的 x || (x && y) 相当于 x 如果是这样,那条法律叫什么?我什至不知道如何通过 Google 搜索该信息。 最佳答案 它被称为 Redundancy Law . A +
是否有任何现有的方法或功能模块可以有效地翻转 boolean 值? 如果我必须定义自己的实用方法,我想出了一个简单的实现,但我想知道这是否是最有效的方法: IF iv_bool = abap_true
我有这个表达式:X'YZ'+X'YZ+XY'Z'+XYZ'+XYZ('表示不是)我知道答案是 Y+XZ' 但我陷入了最后一部分。有人可以帮我吗? 这是我到目前为止得到的: X'YZ' + X'YZ +
openCL 支持 boolean 变量吗?我目前正在使用 JOCL (java) 编写我的 openCL 调用代码,但我没有看到任何有关 boolean 值的信息。 最佳答案 tl;dr:是的,但您
我认为这是对的 x || (x && y) 相当于 x 如果是这样,那条法律叫什么?我什至不确定我会如何使用 Google。 最佳答案 它叫做 Redundancy Law . A + A·B = A
我有一些功能,例如 (A and ( B or c)) or (D and E and (F or H or R or P ))) 我想将该函数转换为仅包含 and 操作的函数(当然如果可能的话)我发
我参加了编程面试,由 3 名面试官组成,每人 45 分钟。虽然前两位面试官给了我 2-3 个简短的编码问题(即反向链表、使用 rand(5) 实现 rand(7) 等),但第三位面试官使用了整个时间段
如果我只想检查某事是否不可能(即,我不会使用类似 if(possible) 的东西),我应该将 boolean 值命名为 notPossible并使用 if(notPossible)或者我应该命名它p
我是一名优秀的程序员,十分优秀!