none
IEqualityComparer and GetHashCode in C# RRS feed

  • Question

  • Hello,

    I should return the following code but How can write these? because we can return one of these.

    return obj.Name.GetHashCode();

    and

                return obj.Family.GetHashCode();

    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Text;
    
    namespace ConsoleApp2
    {
        class CustomerNameComparer : IEqualityComparer<Customer>
        {
            public bool Equals(Customer x, Customer y)
            {
                return (x.Name == y.Name) && (x.Family == y.Family);
            }
    
            public int GetHashCode([DisallowNull] Customer obj)
            {
                return obj.GetHashCode();
            }
        }
    }
    


    • Edited by Arash_89 Saturday, February 1, 2020 7:38 PM
    Saturday, February 1, 2020 7:28 PM

Answers

  • Hello,

    Perhaps this is what you are looking for?

    using System.Collections.Generic;
    
    namespace CustomersExample
    {
        public class Customer
        {
            public string Name { get; set; }
            public string Family { get; set; } 
        }
        public class CustomerNameComparer : IEqualityComparer<Customer> 
        {
            public bool Equals(Customer customer1, Customer customer2)
            {
                return customer1.Name == customer2.Name;
            }
    
            public int GetHashCode([DisallowNull] Customer customer)
            {
                return new { customer.Name}.GetHashCode();
            }
        }
        public class CustomerFamilyComparer : IEqualityComparer<Customer>
        {
            public bool Equals(Customer customer1, Customer customer2)
            {
                return customer1.Family == customer2.Family;
            }
    
            public int GetHashCode([DisallowNull] Customer customer)
            {
                return new { customer.Family }.GetHashCode();
            }
        }
    }
    

    Example 1 by Name

    var customers = new List<Customer>()
    {
        new Customer() { Name = "Karen", Family = "Payne" } ,
        new Customer() { Name = "Mary", Family = "Payne" } ,
        new Customer() { Name = "Karen", Family = "Payne" }
    } ;
    
    var result = customers.Distinct(new CustomerNameComparer());

    Example 2 by Family

    var customers = new List<Customer>()
    {
        new Customer() { Name = "Bill", Family = "Gallagher" } ,
        new Customer() { Name = "Mary", Family = "Payne" } ,
        new Customer() { Name = "Jim", Family = "Gallagher" }
    };
    
    var result = customers.Distinct(new CustomerFamilyComparer());
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Arash_89 Saturday, February 1, 2020 11:02 PM
    Saturday, February 1, 2020 10:21 PM
    Moderator