首页 > 代码库 > C# HowTo 重写Equals和GetHashCode

C# HowTo 重写Equals和GetHashCode

直接上码:

    public class MyObject    {        public string name;        public override bool Equals(object obj)        {            if (ReferenceEquals(null, obj)) return false;            if (ReferenceEquals(this, obj)) return true;            if (obj.GetType() != this.GetType()) return false;            return ((MyObject)obj).name == this.name;        }        public override int GetHashCode()        {            return (name != null ? name.GetHashCode() : 0);        }    }

 

C# HowTo 重写Equals和GetHashCode