C# 使用 == 比较两个结构体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15199026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:25:54  来源:igfitidea点击:

Comparing two structs using ==

c#structequality

提问by JMK

I am trying to compare two structs using equals (==) in C#. My struct is below:

我试图在 C# 中使用 equals (==) 比较两个结构。我的结构如下:

public struct CisSettings : IEquatable<CisSettings>
{
    public int Gain { get; private set; }
    public int Offset { get; private set; }
    public int Bright { get; private set; }
    public int Contrast { get; private set; }

    public CisSettings(int gain, int offset, int bright, int contrast) : this()
    {
        Gain = gain;
        Offset = offset;
        Bright = bright;
        Contrast = contrast;
    }

    public bool Equals(CisSettings other)
    {
        return Equals(other, this);
    }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }

        var objectToCompareWith = (CisSettings) obj;

        return objectToCompareWith.Bright == Bright && objectToCompareWith.Contrast == Contrast &&
               objectToCompareWith.Gain == Gain && objectToCompareWith.Offset == Offset;

    }

    public override int GetHashCode()
    {
        var calculation = Gain + Offset + Bright + Contrast;
        return calculation.GetHashCode();
    }
}

I am trying to have struct as a property in my class, and want to check to see if the struct is equal to the value I am trying to assign it to, before I go ahead and do so, so I am not indicating the property has changed when it hasn't, like so:

我正在尝试将 struct 作为我的类中的一个属性,并想检查该 struct 是否等于我尝试分配给它的值,然后再继续执行此操作,因此我没有指明该属性没有发生变化时发生了变化,如下所示:

public CisSettings TopCisSettings
{
    get { return _topCisSettings; }
    set
    {
        if (_topCisSettings == value)
        {
            return;
        }
        _topCisSettings = value;
        OnPropertyChanged("TopCisSettings");
    }
}

However, on the line where I check for equality, I get this error:

但是,在检查相等性的行中,我收到此错误:

Operator '==' cannot be applied to operands of type 'CisSettings' and 'CisSettings'

运算符“==”不能应用于“CisSettings”和“CisSettings”类型的操作数

I can't figure out why this is happening, could somebody point me in the right direction?

我无法弄清楚为什么会发生这种情况,有人可以指出我正确的方向吗?

采纳答案by Jens Kloster

You need to overload the ==and !=operators. Add this to your struct:

您需要重载==!=运算符。将此添加到您的struct

public static bool operator ==(CisSettings c1, CisSettings c2) 
{
    return c1.Equals(c2);
}

public static bool operator !=(CisSettings c1, CisSettings c2) 
{
   return !c1.Equals(c2);
}

回答by Hans Ke?ing

When you override the .Equals method, the ==operator isn't automatically overloaded. You need to do that explicitly.

当您覆盖 .Equals 方法时,==运算符不会自动重载。你需要明确地做到这一点。

See also Guidelines for Overriding Equals() and Operator ==.

另请参阅覆盖 Equals() 和 Operator == 的指南

回答by Grant Thomas

You don't implement explicitlyan equality operator, so ==is not defined particularly for the type.

你不明确实现平等操作,所以==没有为类型特别规定。

回答by Denys Denysenko

You should overload your operator is some way like this:

你应该像这样重载你的运营商:

public static bool operator ==(CisSettings a, CisSettings b)
{
    return a.Equals(b);
}

回答by Chen

You need to override operator == explicitly.

您需要显式覆盖运算符 ==。

public static bool operator ==(CisSettings x, CisSettings y) 
{
   return x.Equals(y);
}

By the way, you'd better put the comparing code in public bool Equals(CisSettings other), and let bool Equals(object obj)call bool Equals(CisSettings other), so that you can gain some performance by avoiding unnecessary type check.

顺便说一句,你最好把比较代码放在public bool Equals(CisSettings other),让bool Equals(object obj)调用bool Equals(CisSettings other),这样你就可以通过避免不必要的类型检查来获得一些性能。

回答by Xaruth

you must overload "==" operator, but also overload "!=" operator. (Look at this Note)

您必须重载“==”运算符,但也必须重载“!=”运算符。(看这个笔记

For overloading operator, see this page

有关重载运算符,请参阅此页面