最佳答案
我试图比较 C # 中使用 equals (= =)的两个 struct,我的 struct 如下:
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 作为类中的一个属性,并且想要检查 struct 是否等于我试图赋值给它的值,然后继续执行操作,所以我并没有指示属性在没有改变的情况下发生了改变,像这样:
public CisSettings TopCisSettings
{
get { return _topCisSettings; }
set
{
if (_topCisSettings == value)
{
return;
}
_topCisSettings = value;
OnPropertyChanged("TopCisSettings");
}
}
然而,在检查相等性的那一行,我得到了这个错误:
运算符“ = =”不能应用于“ CisSettings”类型的操作数和 “ CisSettings”
我不明白为什么会发生这种事,谁能给我指个正确的方向?