C# 使用==

C# 使用==,c#,struct,equality,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 { g

我试图用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();
    }
}
但是,在检查相等性的行中,我得到以下错误:

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


我不明白为什么会发生这种情况,有人能给我指出正确的方向吗?

你需要重载
==
=运算符。将此添加到您的
结构中

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

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

重写
.Equals()
方法时,
=
运算符不会自动重载。你需要明确地这样做

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

另请参见或。

您没有一个,因此
==
不是专门为类型定义的。

您应该重载运算符的方式如下:

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

您需要显式重写运算符==

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)
,这样您就可以通过避免不必要的类型检查来获得一些性能。

您必须重载“==”操作符,但也重载“!=”操作符。()


对于重载运算符,

创建一个方法并将两个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;
}
public保存返回值更大(联机保存,本地保存)
{
Save DataToSave=new Save();
DataToSave.Score=local.Score
@JMK,可能是因为您没有覆盖它…:)
if(obj==null | | GetType()!=obj.GetType())
是一种非常奇怪的编写
if(!(obj是CisSettings))
的方法。此外,逻辑也位于错误的位置:将特定于类型的逻辑放在
Equals(CisSettings)
中,并让
Equals(object)
调用它,而不是反过来调用它。另外,对32位整数调用
GetHashCode
是不必要的;32位整数是它自己的散列码。此外,如果四个数字的值趋于相似,则散列码的分布不好。请不要忘记重载“!=”运算符;)“可能”与此无关;不重写!=而重写==是非法的。如果你关心性能,你应该考虑自己执行<代码>=< /代码>运算符。上述实现会导致装箱,如中所述,装箱“计算代价高昂”。特别是因为您可以比较结构所包含的4个整数。出于好奇,是否存在希望
Equals()
==
返回不同值的情况?我试图理解为什么它们有不同的实现,因为它们看起来像是完美的同义词。@Nerrolken.Equals()通常用于值相等,而==保留为引用相等