码迷,mamicode.com
首页 > Windows程序 > 详细

C# HowTo 重写Equals和GetHashCode

时间:2014-10-23 08:09:37      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   sp   div   log   bs   ef   as   

直接上码:

    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

标签:style   blog   color   sp   div   log   bs   ef   as   

原文地址:http://www.cnblogs.com/timba/p/4044826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!