问题描述
当我调试的code几行字,问我到底为什么它不工作,我无意中发现了这个情况......
When I was debugging a few lines of code and asking me why on earth it wasn't working I have stumbled on this situation ...
if(answer.AnswerID == null)
{
// do something
}
当实际上它应该是这样的:
When in fact it should be this way:
if(answer == null)
{
// do something
}
-
答案
的类型是答的对象 - 类
-
AnswerID
是一个类型的属性长
。 answer
is an object of the typeAnswer - a class
.AnswerID
is a property of typelong
.
奇怪的是,如果你尝试这样的:
The weird thing is that if you try something like this:
long myLongValue = null;
,编译器会告诉你一个错误:
The compiler will show you an error:
Connot convert null to long ...
所以我的问题是:为什么我没有拿到A 编译错误当我试图用<$ C $比较一长型
C>空?
So my question is: Why did I not get a compile error when I was trying to compare a long type
with null
?
EDITED
此问题不是关于可空
类型。
我问为什么.NET可以让我长的变量,空进行比较。我说的是长型
而不是关于长?键入
。
I'm asking WHY .NET allows me to compare a long variable with null. I'm talking about long type
and not about long? type
.
推荐答案
作为@Tim指出,你不会得到一个错误以下code:
As @Tim pointed out, you won't get an error for the following code:
long foo = 42;
if (foo == null) { }
您会得到一个警告:
的前pression的结果总是'假',因为类型长的值是永远不会等于零式的多长时间?
这给出了一个警告,而不是因为的,在C#语言规范这样定义的:
This gives a warning instead of an error because of lifted operators, defined in the C# language specification as such:
翘起运营商 的许可证predefined和非空值类型进行操作的用户定义的运营商也可以与这些类型的空形式使用。 [...]对于相等运算符
== !=
如果操作数类型都为非可空值类型,如果结果类型是布尔运算符的提升形式存在。 的提升形式是通过将一个单一的构造?修改为每个操作数的类型。提升运算符认为两个空值相等,空值不等于任何非空值。如果两个操作数都是非空,提升运算进行解包操作数和应用基础运算符产生布尔结果。
a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator considers two null values equal, and a null value unequal to any non-null value. If both operands are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.
在这种情况下,基础运算符是的:
The "underlying operator" in this case is the predefined value type long
's ==
operator:
有关predefined值类型,如果它的操作数的值相等等号(= =)返回true,否则为false。
由于富
隐式转换(predefined的非空值类型进行操作的隐式转换也可以用这些类型的可空形式使用。)的和空
的文字也被隐式转换(隐式转换存在从null文本到任何可空类型。),前pression:
Because foo
is implicitly converted ("Predefined implicit conversions that operate on non-nullable value types can also be used with nullable forms of those types.") and the null
literal is also implicitly converted ("An implicit conversion exists from the null literal to any nullable type."), the expression:
(long)foo == null
变成了:
(long?)foo == (long?)null
其中,由于富
的类型是长
,因此总是有一个值,总是返回false并荣获吨甚至适用长
的 ==
运营商。
Which, given foo
is of type long
and thus always has a value, always returns false and won't even apply long
's ==
operator.
我不能完全肯定,但我怀疑这存在,使没有明确的铸件可空和非空的值进行比较:
I'm not entirely sure, but I suspect this to exist to enable comparison between nullable and non-nullable values without explicit casting:
long? foo = 42;
long bar = 42;
Console.WriteLine(foo == bar); // true
foo = null;
Console.WriteLine(bar == foo); // false
如果这是由语言上面规定不处理的,你会得到的运算符 ==
不能应用于类型的操作数长?
和长
的,因为的没有一个 ==
运营商,和长
不具有 ==
运营商接受长?
。
If this wasn't handled by the language as specified above, you'd get "Operator ==
cannot be applied to operands of type long?
and long
", because Nullable<T>
doesn't have an ==
operator, and long
doesn't have an ==
operator accepting a long?
.
这篇关于为什么英寸长度值QUOT;等于空是允许的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!