gpt4 book ai didi

c# - 为什么不能使用 is 运算符来区分 bool 和 Nullable

转载 作者:可可西里 更新时间:2023-11-01 08:08:12 25 4
gpt4 key购买 nike

我遇到了这个,很好奇为什么不能使用 is运算符区分 boolNullable<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!

为什么两者都要 boolNullable<bool>相互匹配?

我尝试了什么;

  • 我已经查阅了 Nullable 的文档, is , switch和模式匹配。
  • 我认为这可能与 unboxing 有关传递给方法时的值是多少?

我认为它可能是 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>

我不是要解决问题。只是对它为何以这种方式工作感兴趣。

到目前为止的答案表明它是 implicitexplicit导致此行为的转换,但我无法使用以下示例进行复制;

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.

引用转换、装箱转换和拆箱转换是由框架决定的吗?

最佳答案

原因boolNullable<bool>传递给您的方法时表现相同是因为每​​当您将 Nullable<T> 装箱时它实际上并没有装箱可为空的值,而是解开可为空的值并将其装箱。如果可为 null 的值为 null,那么您最终只会得到 null , 而不是盒装 Nullable<T>其中 HasValuefalse .

如果您装箱一个非空值,它只会装箱 ValueNullable<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 TD之前定义为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com