== and != operator overloading in Unity3D C# syntax

foreword

In coding, in order to improve the readability of the code, we often overload operators to encapsulate some operations. Good operator overloading can greatly improve the readability of the code. This article will talk about == and != operator overloading.

overloaded function

In C#, when overloading, you need to use the operator keyword to declare:

public static bool operator 运算符(object1 左对象, object 右对象){}

The binary operator needs to input two parameters when filling in the parameters. The first parameter is the parameter of the operator, the second is the right parameter of the operator, and the final return value is the custom result of the operator.
Here we only talk about == and != overloaded operators, because these two operators appear in pairs, and there are similar < and > symbols.

public struct TempStruct
{
    public int x = 10;
    public static bool operator ==(TempStruct a,TempStruct b)
    {
        if(a.x == b.x)
        {
            return true;
        }
        return false;
    }
}
public static bool operator!=(TempStruct a,TempStruct b)
{
    return !(a == b);//直接通过使用==的逻辑,避免写重复代码
}

Here, when dealing with !=, you can directly use the logic of == to simplify the code. The same handling can also be placed in overloads that handle < and >.

Later, when we compare the two structures, we only need to use the following code to compare their worth.

TempStruct a,b;
if(a == b){}

Upgrade processing

Do you think this is the end?

For the overloads of == and !=, if the input is empty, the above judgment will report an error. Then the simplest modification is to add an empty judgment to the function:

public struct TempStruct
{
    public int x = 10;
    public static bool operator ==(TempStruct a,TempStruct b)
    {
        if(a == null)//会出现递归调用
        {
            if(b == null) return truel
            return false;
        }
        if(a.x == b.x)
        {
            return true;
        }
        return false;
    }
}
public static bool operator!=(TempStruct a,TempStruct b)
{
    return !(a == b);//直接通过使用==的逻辑,避免写重复代码
}

This is very fast to write, and also considers the situation of two empty objects, but this will fall into an infinite loop, because recursive calls appear when making short judgments.

To avoid this, the handling of nulls needs to be done in a different way:

public struct TempStruct
{
    public int x = 10;
    public static bool operator ==(TempStruct a,TempStruct b)
    {
        if(object.Equal(a,null) ||object.Equal(b,null) )//会出现递归调用
        {
            return object.Equal(a,b);
        }
        if(a.x == b.x)
        {
            return true;
        }
        return false;
    }
}
public static bool operator!=(TempStruct a,TempStruct b)
{
    return !(a == b);//直接通过使用==的逻辑,避免写重复代码
}

Note that the object.Equal(a,b) method is used here for null judgment, avoiding the use of overloaded operators.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711525&siteId=291194637