简体   繁体   English

是Nullable <int> “预定义值类型” - 或者Equals()和==如何在这里工作?

[英]Is Nullable<int> a “Predefined value type” - Or how does Equals() and == work here?

For my own implementation of an Equals() method, I want to check a bunch of internal fields. 对于我自己实现的Equals()方法,我想检查一堆内部字段。 I do it like this: 我是这样做的:

...
_myNullableInt == obj._myNullableInt &&
_myString == obj._myString &&
...

I would assume, that this compares the values, including null, for equality not the object address (as a reference euqality compare operation would) because: 我假设,这比较值,包括null,对于相等而不是对象地址(作为参考euqality比较操作),因为:

It is said so for "predefined value types" in this MSDN doc here . 对于此MSDN文档中的 “预定义值类型” ,可以这样说 I assume Nullable<int> is such a "predefined value type" because of it is in the System Namespace according to this MSDN doc . 我假设Nullable<int>是这样的“预定义值类型”,因为它根据此MSDN文档位于System命名空间中。

Am I right to assume that the VALUES are compared here? 我是否正确地假设VALUES在这里进行比较?

Note: Unit tests showed "Yes", but I wanted to be reassured by others with this question, just in case I missed something. 注意:单元测试显示“是”,但我希望其他人对这个问题感到放心,以防我错过了什么。

In C#, there's a concept called "Lifted Operators", described in section 7.3.7 of the language specification ( Version 5 download ): 在C#中,有一个名为“Lifted Operators”的概念,在语言规范( 版本5下载 )的7.3.7节中描述:

Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. 提升的运算符允许在非可空值类型上运行的预定义和用户定义的运算符也可以与这些类型的可空形式一起使用。 Lifted operators are constructed from predefined and user-defined operators that meet certain requirements, as described in the following 提升运营商由满足特定要求的预定义和用户定义的运营商构建,如下所述

And specifically: 具体而言:

For the equality operators 对于等于运算符

==  !=

a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. 如果操作数类型都是非可空值类型并且结果类型是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. 如果两个操作数都为非null,则提升的运算符将解包操作数并应用基础运算符以生成bool结果。

So, since there's an == operator defined between int s, there's also one defined for int? 那么,既然在int之间定义了一个==运算符,那么还有一个为int?定义的运算符int? s 小号

If you compare those values it will actually call the Nullable<T>.Equals method, since both values are nullable ints. 如果比较这些值,它实际上将调用Nullable<T>.Equals方法,因为两个值都是可为空的int。

Nullable<T>.Equals will eventually call the == compare keyword of int, if both values are not null. Nullable<T>.Equals最终将调用int的== compare关键字,如果两个值都不为null。 So in the end, it will indeed check the values. 所以最后,它确实会检查这些值。

The code from the Equals method shows this well: Equals方法的代码很好地说明了这一点:

public override bool Equals(object other)
{
    if (!this.HasValue)
    {
        return (other == null);
    }
    if (other == null)
    {
        return false;
    }
    return this.value.Equals(other);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM