我正在尝试在 C# 中使用等于 (==) 比较两个结构。我的结构如下:
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”类型的操作数
我不知道为什么会这样,有人能指出我正确的方向吗?