当给定null时,为什么is运算符会返回false? [英] Why does the is operator return false when given null?

查看:77
本文介绍了当给定null时,为什么is运算符会返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,运算符有点不一致。

It seems to me that the is operator is a bit inconsistent.

bool Test()
{
    // Returns false, but should return true.
    return null is string;
}

人们期望为空值属于任何引用(或可为空)类型。确实,C#语言规范说了一些支持此假设的内容,例如(6.1.6隐式引用转换):

One expects that the null value belongs to any reference (or nullable) type. And indeed, the C# language specification says something which supports this hypothesis, for example (6.1.6 Implicit reference conversions):


转换为:

...

•从空字面量到任何引用类型。

The implicit reference conversions are:
...
• From the null literal to any reference-type.

is 运算符的描述(7.10.10 is运算符)首先说表达式(E为T) E T 的引用转换时,code>将得出true,但是作者通过明确排除 E null 文字或具有 null 的情况, code>值。

The description (7.10.10 The is operator) of the is operator starts by saying that the expression (E is T) will result in true when a reference conversion from E to T exists, but then the authors go on by explicitly excluding the case when E is the null literal or has a null value.

为什么这样做?在我看来,这似乎违反直觉。

Why do they do that? To me it seems counterintuitive.

推荐答案

这个问题是主题

您正盯着空车道。

有人问您车道上可以装本田思域吗?

Someone asks you "can your driveway hold a Honda Civic?"

是。是的。

有人将您指向第二条车道。它也是空的。他们问:我车道上的当前内容是否适合您的车道?

Someone points you at a second driveway. It is also empty. They ask "Can the current contents of my driveway fit in your driveway?"

显然是的。两个车道都是空的!显然,其中一个的内容可以适合另一个,因为其中任何一个都不存在。

Yes, obviously. Both driveways are empty! So clearly the contents of one can fit in the other, because there are no contents of either in the first place.

有人问您您的车道上是否装有本田思域? ?

Someone asks you "Does your driveway contain a Honda Civic?"

不,不是。

您以为运算符回答第二个问题:给定该值,它适合该类型的变量吗?空引用是否适合该类型的变量?是的。

You're thinking that the is operator answers the second question: given this value, does it fit in a variable of that type? Does a null reference fit into a variable of this type? Yes it does.

这不是运算符回答的问题。 运算符的问题是第三个问题。 y是X 不会询问 y 是<$ c $类型变量的合法值吗? c> X ,它询问 y 是否有效引用了类型为 X ?因为空引用不是对 any 类型的 any 对象的有效引用,所以答案为否 。那条车道是空的。它不包含本田思域。

That is not the question that the is operator answers. The question that the is operator answers is the third question. y is X does not ask "is y a legal value of a variable of type X?" It asks "Is y a valid reference to an object of type X?" Since a null reference is not a valid reference to any object of any type, the answer is "no". That driveway is empty; it doesn't contain a Honda Civic.

另一种查看方式是 y是X 回答问题如果我说 y为X ,我会得到非空结果吗?如果y为null,显然答案是否定的!

Another way to look at it is that y is X answers the question "if I said y as X, would I get a non-null result? If y is null, clearly the answer is no!

深入探讨您的问题:


一个期望空值属于任何引用(或可为空)类型

One expects that the null value belongs to any reference (or nullable) type

一个将隐式假定为类型是一组,并且值y与类型X的变量的赋值兼容性等于或小于检查y是否为a set x 的成员。

One would be implicitly assuming that a type is a set of values, and that assignment compatibility of a value y with a variable of type X is nothing more nor less than checking whether y is a member of set x.

尽管这是一种非常常见的类型查看方式,但并非 only 方式空引用是C#中没有类型的成员; 赋值兼容性不是 仅仅检查一个集合查看它是否包含值。仅仅因为null引用与引用类型X的变量 assignmentcompatible 并不意味着null是类型X的成员。与is兼容关系和是type的成员 关系显然有很多重叠,但是在CLR中它们并不相同。

Though that is an extremely common way of looking at types, that is not the only way of looking at types, and it is not the way that C# looks at types. Null references are members of no type in C#; assignment compatibility is not merely checking a set to see if it contains a value. Just because a null reference is assignment compatible with a variable of reference type X does not mean that null is a member of type X. The "is assignment compatible with" relation and the "is a member of type" relation obviously have a lot of overlap, but they are not identical in the CLR.

如果您对类型理论的兴趣引起了您的兴趣,请查看我最近关于该主题的文章:

If musings about type theory interest you, check out my recent articles on the subject:

您称其为类型是什么?第一部分

这是什么东西,您称之为类型?第二部分

这篇关于当给定null时,为什么is运算符会返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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