bgh

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 重写Equals

Posted on 2019-01-14 17:51  bgh  阅读(919)  评论(0编辑  收藏  举报
public class PerformanceRank
        {
            public int Rank { get; set; }

            public string Eid { get; set; }

            public string Name { get; set; }

            public decimal Money { get; set; }

            //重写Equals方法

            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return false;
                }
                if ((obj.GetType().Equals(this.GetType())) == false)
                {
                    return false;
                }
                PerformanceRank temp = null;
                temp = (PerformanceRank)obj;

                //因为取差集,不用rank,就不写rank
                return this.Eid.Equals(temp.Eid) && this.Name.Equals(temp.Name) && this.Money.Equals(temp.Money);                

            }

            //重写GetHashCode方法(重写Equals方法必须重写GetHashCode方法,否则发生警告

            public override int GetHashCode()
            {
                return this.Eid.GetHashCode() + this.Name.GetHashCode() + this.Money.GetHashCode();              
            }
        }

  

    wtr.ItemsSource = week_top_rank;
    //wbr.ItemsSource = week_bottom_rank.Except(week_top_rank).ToList(); //排除重复的数据
    wbr.ItemsSource = week_bottom_rank.Where(d => !week_top_rank.Contains(d)); //排除重复的数据
    mtr.ItemsSource = month_top_rank;
    mbr.ItemsSource = month_bottom_rank.Except(month_top_rank).ToList(); //排除重复的数据