C#HowTo EqualsとGetHashCodeの書き換え

1914 ワード

ダイレクトコード:
    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);

        }

    }