使用==比较两个结构

时间:2013-03-04 10:09:30

标签: c# struct equality

我试图在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();
    }
}

我正在尝试将struct作为我的类中的属性,并且想要检查结构是否等于我尝试将其分配给的值,然后我继续这样做,所以我不是如果没有,则表示该属性已更改,如下所示:

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

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

  

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

我无法弄清楚为什么会这样,有人能指出我正确的方向吗?

7 个答案:

答案 0 :(得分:74)

您需要重载==!=运算符。将其添加到您的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);
}

答案 1 :(得分:4)

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

另见Guidelines for Overriding Equals() and Operator ==

答案 2 :(得分:1)

您不是implement explicitly equality operator,因此==并未特别为该类型定义。

答案 3 :(得分:1)

您应该以某种方式重载您的运营商:

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

答案 4 :(得分:1)

您需要显式覆盖operator ==。

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

顺便说一句,您最好将比较代码放在public bool Equals(CisSettings other)中,让bool Equals(object obj)调用bool Equals(CisSettings other),以便通过避免不必要的类型检查来获得一些性能。< / p>

答案 5 :(得分:1)

你必须重载“==”运算符,但也要重载“!=”运算符。 (Look at this Note

对于重载运算符see this page

答案 6 :(得分:1)

制作一个方法,将两个 stuct obj 作为参数进行一一比较

public Save ReturnGreater(Save online,Save local)
{
    Save DataToSave = new Save();
    DataToSave.Score = local.Score < online.Score ? online.Score : local.Score;
    DataToSave.UnlockGuns = local.UnlockGuns < online.UnlockGuns ? online.UnlockGuns : local.UnlockGuns;
    DataToSave.UnlockLevels = local.UnlockLevels < online.UnlockLevels ? online.UnlockLevels : local.UnlockLevels;
    DataToSave.TotalLevels = local.TotalLevels;
    DataToSave.RemoveAds = local.RemoveAds;
    return DataToSave;
}