Comparing two structs using ==

前端 未结 6 853
萌比男神i
萌比男神i 2020-12-11 00:29

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

public struct CisSettings : IEquatable
{
    public int Ga         


        
6条回答
  •  不知归路
    2020-12-11 01:03

    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.

提交回复
热议问题