zoukankan      html  css  js  c++  java
  • IEqualityComparer<TSource> 比较规则

        class Program
        {
            static void Main(string[] args)
            {
                List<People> peoples = new List<People>
                {
                    new  People{ ID=1,Name="xxx" },
                    //new  People{ ID=1,Name="xxx" },
                    new  People{ID=2,Name="yyy" },
                };
    
                peoples = peoples.Distinct(new Filter()).ToList();
        
                Console.WriteLine("Hello World!" + peoples.Count);
            }
        }
        
        public class Filter : IEqualityComparer<People>
        {
            public bool Equals([AllowNull] People x, [AllowNull] People y)
            {
                Console.WriteLine($"Equals->{x.ID},{y.ID}");
                return x.ID == y.ID;
            }
    
            public int GetHashCode([DisallowNull] People obj)
            {
                Console.WriteLine($"GetHashCode->{obj.ID}");
                return obj.ID;
            }
        }
    

    IEqualityComparer 比较规则:

    1. 如果 HashCode 不同,则不运行 Equals , 直接判定为不同
    2. 如果 HashCode 相同,则运行 Equals , 根据 Equals 判定相不相不同
  • 相关阅读:
    InterLockedIncrement and InterLockedDecrement
    bzoj2763
    bzoj1922
    bzoj1705
    bzoj1040
    bzoj3039
    bzoj1801
    bzoj2565
    bzoj1976
    一类最小割bzoj2127,bzoj2132 bzoj3438
  • 原文地址:https://www.cnblogs.com/chasingdreams2017/p/13972414.html
Copyright © 2011-2022 走看看