- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我遇到了这个,很好奇为什么不能使用 is
运算符区分 bool
和 Nullable<bool>
?示例;
void Main()
{
bool theBool = false;
Nullable<bool> theNullableBoolThatsFalse = false;
Nullable<bool> theNullableBoolThatsNull = null;
void WhatIsIt(object value)
{
if(value is bool)
Console.WriteLine(" It's a bool!");
if(value is Nullable<bool>)
Console.WriteLine(" It's a Nullable<bool>!");
if(value is null)
Console.WriteLine(" It's a null!");
}
Console.WriteLine("Considering theBool:");
WhatIsIt(theBool);
Console.WriteLine("Considering theNullableBoolThatsFalse:");
WhatIsIt(theNullableBoolThatsFalse);
Console.WriteLine("Considering theNullableBoolThatsNull:");
WhatIsIt(theNullableBoolThatsNull);
}
调用 Main()
给予;
Considering theBool:
It's a bool!
It's a Nullable<bool>!
Considering theNullableBoolThatsFalse:
It's a bool!
It's a Nullable<bool>!
Considering theNullableBoolThatsNull:
It's a null!
我希望;
Considering theBool:
It's a bool!
Considering theNullableBoolThatsFalse:
It's a Nullable<bool>!
Considering theNullableBoolThatsNull:
It's a null!
为什么两者都要 bool
和 Nullable<bool>
相互匹配?
我尝试了什么;
Nullable
的文档, is
, switch
和模式匹配。我认为它可能是 Nullable
独有的因为我不会遇到其他泛型类型的相同问题。例如;
void Main()
{
bool theBool = false;
List<bool> theListOfBool= new List<bool>();
void WhatIsIt(object value)
{
if(value is bool)
Console.WriteLine(" It's a bool!");
if(value is List<bool>)
Console.WriteLine(" It's a List<bool>!");
}
Console.WriteLine("Considering theBool:");
WhatIsIt(theBool);
Console.WriteLine("Considering theListOfBool:");
WhatIsIt(theListOfBool);
}
给予;
Considering theBool:
It's a bool!
Considering theListOfBool:
It's a List<bool>
我不是要解决问题。只是对它为何以这种方式工作感兴趣。
到目前为止的答案表明它是 implicit
和 explicit
导致此行为的转换,但我无法使用以下示例进行复制;
class A
{
public static implicit operator A(B value) => new A();
public static explicit operator B(A value) => new B();
}
class B
{
public static implicit operator A(B value) => new A();
public static explicit operator B(A value) => new B();
}
static void Main(string[] args)
{
var a = new A();
var b = new B();
void WhatIsIt(object value)
{
if (value is A)
Console.WriteLine(" It's a A!");
if (value is B)
Console.WriteLine(" It's a B!");
}
Console.WriteLine("Considering a;");
WhatIsIt(a);
Console.WriteLine("Considering b;");
WhatIsIt(b);
}
给予;
Considering a;
It's a A!
Considering b;
It's a B!
is
的文档说:
It only considers reference conversions, boxing conversions, and unboxing conversions; it does not consider user-defined conversions or conversions defined by a type's implicit and explicit operators. The following example generates warnings because the result of the conversion is known at compile-time. Note that the is expression for conversions from int to long and double return false, since these conversions are handled by the implicit operator.
引用转换、装箱转换和拆箱转换是由框架决定的吗?
最佳答案
原因bool
和 Nullable<bool>
传递给您的方法时表现相同是因为每当您将 Nullable<T>
装箱时它实际上并没有装箱可为空的值,而是解开可为空的值并将其装箱。如果可为 null 的值为 null,那么您最终只会得到 null
, 而不是盒装 Nullable<T>
其中 HasValue
是false
.
如果您装箱一个非空值,它只会装箱 Value
的 Nullable<T>
.所以从WhatIsIt
的角度来看,前两个调用几乎无法区分,因为传入的值完全相同。
剩下的问题就是为什么两者 is
支票返回 true
,即使在这两种情况下传入的是一个装箱的 bool 值,而不是 Nullable<T>
. C# 语言规范第 7.10.10 节回答了这个问题:
If T is a nullable type, the result is true if D is the underlying type of T.
在这种情况下,正在考虑 E is T
和 D
之前定义为 E
的计算值其中:
If the type of E is a nullable type, D is the underlying type of that nullable type.
这意味着 is
运算符明确定义为将可空类型视为等同于它们的基础类型,无论您如何混合和匹配被检查的实际值以及您使用可空值检查的类型以及该可空的基础类型.
关于c# - 为什么不能使用 is 运算符来区分 bool 和 Nullable<bool>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54128310/
我有一个带有列的表提供者 implied(tiny int)(something like nullable bool) provi
我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个: public bool
这是我的完整代码... import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import
我有一个输入 list类型 [Maybe SomeType]和一个谓词 p类型 SomeType -> Bool ,我想回答这个问题“谓词 p 是否适用于所有碰巧在输入中的 SomeType ?”。
使用 !!x 有什么区别吗?对比(bool)x ? 假设__STDC_VERSION__ >= 199901L和 #include 他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不
我正在编写一些 C++ 代码,我想调用两个函数(checkXDirty 和 checkYDirty),并返回 true如果任一返回 true。即使一个返回 true 我也需要评估两者,所以我的第一个想
我注意到 bool在 QtCreator 中以不同于其他类型的颜色突出显示: 只有在包含某些 header 时才会发生这种情况,最终我将其追踪到 . QtCreator 的代码检查器似乎无法手动跟踪
有一个函数: func (first: Int) -> Int -> Bool -> String { return ? } 返回值怎么写?我对上面 func 的返回类型感到很困惑。 最
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我已经创建了任务函数来验证我的 json 文件。一切正常,直到我没有使用结果。当我试图从 async task function 获得结果时它显示错误为 Cannot implicitly conve
我有一个函数 func login (parameters: [(String, Any)], completion: @escaping (Bool) -> Vo
我正在处理最近从 X/Motif 转移到 Qt 的 C++ 代码库。我正在尝试编写一个 Perl 脚本,它将用 bool 替换所有出现的 Boolean(来自 X)。该脚本只是做了一个简单的替换。 s
嗨,我正尝试创建一个Visiblity小部件,如果用户在Firebase数据库阵列上,该小部件将显示。看起来像这样(成员数组): 如您所见,我创建了一个StreamBuilder,如果当前用户的用户名
我创建了如下的rest api方法, Future activateAccount(int id, int code) async{ final body = {"code": '$c
在我的Flutter应用中,我有一个返回Future的函数,但我想将结果作为Stream。这是函数: Future isGpsOn() async { if (await Geolocat
我可以看到 BOOLEAN 覆盖了 __visit_name__ class BOOLEAN(Boolean): __visit_name__ = 'BOOLEAN' 控制调度员选择的访问者方
考虑以下代码: bool x; bool? y = null; x = y?? true; 将 bool? 分配给 bool 是一个编译时错误,但上面的代码在编译和运行时都成功了。为什么?尽管第三条语
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我想拍一张bool来自Vec并在 if 语句中进行比较。如何解决以下错误? | 7 | if cell { | ^^^^ expected
我在我的应用程序崩溃跟踪工具中发现了一些崩溃。基本上我有一个 tabBarController,其中一个选项卡有一个嵌入式 UIWebView,另一个选项卡有一个带有 UITableView 的 Co
我是一名优秀的程序员,十分优秀!