如何将HashSet <t>与对象一起使用以过滤唯一对象

时间:2019-05-06 11:24:15

标签: c#

由于HashSet<T>在过滤后提供了唯一的项目。我想将其与某些给定的类对象一起使用,但是它不起作用,但是它与int之类的原始类型一起使用。我的班级现在正在实现IEqualityComparer.GetHashCode方法在这里很重要。

        class Address : IEqualityComparer<Address>
            {
                public int PinCode { get; set; }
                public string LocalAddress { get; set; }

                public bool Equals(Address x, Address y)
                {
                    if (x is null && y is null)
                        return true;
                    else if (x is null || y is null)
                        return false;
                    else if ((x.PinCode == y.PinCode) && (x.LocalAddress == y.LocalAddress))
                        return true;
                    else
                        return false;
                }

                public int GetHashCode(Address obj) =>
                   obj.LocalAddress.GetHashCode() * 17 + obj.PinCode.GetHashCode();

            }

    class Person : IEqualityComparer<Person>
        {
            public bool Equals(Person x, Person y)
            {
                if (x is null && y is null)
                    return true;
                else if (x is null || y is null)
                    return false;
                else if ((x.Name == y.Name) && (x.Address.LocalAddress == y.Address.LocalAddress) && (x.Address.PinCode == y.Address.PinCode))
                    return true;
                else
                    return false;
            }

            public int GetHashCode(Person obj) =>
                obj.Name.GetHashCode() * 17 + obj.Address.PinCode.GetHashCode() + obj.Address.LocalAddress.GetHashCode();

            public string Name { get; set; }
            public Address Address { get; set; }
        }

class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>()
                {
                    new Person
                    {
                        Name="Amar",
                        Address=new Address
                        {
                            PinCode=500018,
                            LocalAddress="Hyderabad"
                        }
                    },
                    new Person
                    {
                        Name="Amar",
                        Address=new Address
                        {
                            PinCode=500018,
                            LocalAddress="Hyderabad"
                        }
                    },
                    new Person
                    {
                        Name="Alok",
                        Address=new Address
                        {
                            PinCode=500018,
                            LocalAddress="Hyderabad"
                        }
                    }
                };

            HashSet<Person> uniquePeople = new HashSet<Person>(new Person());
            uniquePeople.UnionWith(people);

            Console.ReadLine();
        }

实际结果:-计数为2 预期结果:-计数应为2         }

0 个答案:

没有答案