简体   繁体   English

这个重载是什么意思?

[英]What does this overload mean?

Can someone explain me what does this overload mean? 有人可以解释一下这个重载是什么意思吗?

public static bool operator ==(Shop lhs, Shop rhs)
{
    if (Object.ReferenceEquals(lhs, null))
    {
        if (Object.ReferenceEquals(rhs, null))
        {
            return true;
        }
        return false;
    }

    return lhs.Equals(rhs);
}

I have never seen Object.ReferenceEquals in overload 我从未在重载中看到过Object.ReferenceEquals

This overload was intended to compare two instances of Shop . 此重载旨在比较Shop两个实例。 It uses Object.ReferenceEquals to determine if one of the instances is null . 它使用Object.ReferenceEquals来确定其中一个实例是否为null
It cannot use lhs == null or rhs == null , because this would again invoke the operator == and create an infinite recursion leading to a StackOverflowException . 它不能使用lhs == nullrhs == null ,因为这会再次调用operator ==并创建一个导致StackOverflowException的无限递归。

If both instances are null it returns true (since they are equal). 如果两个实例都为null则返回true(因为它们相等)。
If only one instance is null it returns false (since they are not equal). 如果只有一个实例为null则返回false(因为它们不相等)。
If both instances are not null it returns the result of the Equals implementation of Shop . 如果两个实例都不为null则返回ShopEquals实现的结果。

It is an operator overload (of ==, not method overload of ReferenceEquals ) to check if two instances of type Shop is of equal reference (that is, whether they refer to the same memory address ). 它是一个operator overload (==,而不是ReferenceEquals方法重载)来检查Shop类型的两个实例是否具有相同的引用(即,它们是否引用相同的内存地址 )。

bool result = shop1 == shop2; //shop1 and shop2 are of type Shop 

When declaring == operator, you will also be required to overload its matching (or counter) operator != : 声明==运算符时,还需要重载其匹配(或计数器)运算符!=

public static bool operator ==(Shop lhs, Shop rhs) {
    if (Object.ReferenceEquals(lhs, null)) { //Check if the left-hand-side Shop is null
        if (Object.ReferenceEquals(rhs, null)) {
            return true; //both are null, equal reference
        }
        return false; //lhs is null, but rhs is not (not equal reference)
    }
    return lhs.Equals(rhs); //lhs is not null, thus can call .Equals, check if it is Equals to rhs
}

public static bool operator !=(Shop lhs, Shop rhs) { //the opposite operator
    if (Object.ReferenceEquals(lhs, null)) {
        if (Object.ReferenceEquals(rhs, null)) {
            return false;
        }
        return true;
    }
    return !lhs.Equals(rhs);
}

It is also worth to note that Object.ReferenceEquals(lhs, null) is used instead of lhs == null as the second will lead to another overload == being called till infinite recursion which causes StackOverflowException . 还值得注意的是,使用Object.ReferenceEquals(lhs, null)而不是lhs == null因为第二个将导致另一个重载==被调用直到无限递归 ,这会导致StackOverflowException

They are used like this: 他们像这样使用:

Shop shop1 = new Shop();
Shop shop2 = new Shop();
bool result = shop1 == shop2; //this will return false, since lhs and rhs referring to two different memory address
shop2 = shop1;
result = shop1 == shop2; //this will return true, referring to the same memory location
shop1 = null;
shop2 = null;
result = shop1 == shop2; //this will return true, both are null

Understanding this, you could even create something like this: 理解这一点,你甚至可以创建这样的东西:

public struct MyCrazyInt{ //this will reverse the result of + and -
    private int Value { get; set; }
    public MyCrazyInt(int value) :this() {
        Value = value;
    }

    public bool Equals(MyCrazyInt otherCrazy) {
        return this.Value != otherCrazy.Value; //reverse this result
    }

    public static MyCrazyInt operator +(MyCrazyInt lhs, MyCrazyInt rhs) {
        int lhsVal = lhs.Value;
        int rhsVal = rhs.Value;
        return new MyCrazyInt(lhsVal - rhsVal); //note that direct lhs-rhs will cause StackOverflow
    }

    public static MyCrazyInt operator -(MyCrazyInt lhs, MyCrazyInt rhs) {
        int lhsVal = lhs.Value;
        int rhsVal = rhs.Value;
        return new MyCrazyInt(lhsVal + rhsVal); //note that direct lhs+rhs will cause StackOverflow
    }

    public override string ToString() {
        return Value.ToString();
    }
}

And then use it like this 然后像这样使用它

MyCrazyInt crazyInt1 = new MyCrazyInt(5);
MyCrazyInt crazyInt2 = new MyCrazyInt(3);
MyCrazyInt crazyInt3 = crazyInt1 - crazyInt2; //this will return 8
crazyInt3 = crazyInt1 + crazyInt2; //this will return 2

It's very easy. 这很容易。 "NULL" actually is an object that resides in memory and has a reference and can be set To any object That is a subclass of Base "Object" class. “NULL”实际上是一个驻留在内存中并具有引用的对象,可以设置为任何对象,它是Base“Object”类的子类。

So above code first checks both "Shop" objects are equally null by comparing their reference value to "null" object reference, if both of them are equal to null so they are equal and return True. 因此,上面的代码首先通过将它们的引用值与“null”对象引用进行比较来检查两个“Shop”对象是否为null,如果它们都等于null,则它们相等并返回True。

If just first object is null and second one is not, return false. 如果只有第一个对象为空而第二个对象为空,则返回false。

And finally if first Shop object is not null then the code supposes that the second one is not null and compares their instance against Shop object to check they are equal. 最后,如果第一个Shop对象不为null,则代码假设第二个不为null,并将它们的实例与Shop对象进行比较以检查它们是否相等。

And the main reason we nust use this way to compare null object is because you get a runtime error if you compare null or not instantiated object so we need to override the default "==" operator in this way. 我们必须使用这种方式比较null对象的主要原因是,如果比较null或未实例化的对象,则会出现运行时错误,因此我们需要以这种方式覆盖默认的“==”运算符。

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

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