Comparing two structs using ==

亡梦爱人 提交于 2019-11-28 02:24:12

问题


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

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:

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'

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


回答1:


You need to overload the == and != operators. Add this to your 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);
}



回答2:


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

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




回答3:


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




回答4:


You should overload your operator is some way like this:

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



回答5:


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.




回答6:


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

For overloading operator, see this page



来源:https://stackoverflow.com/questions/15199026/comparing-two-structs-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!