C# 重写Equals

2021-06-20 04:04

阅读:277

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(); //排除重复的数据


评论


亲,登录后才可以留言!