how to get hash of the object?

mc 5,426 Reputation points
2024-09-07T13:11:46.16+00:00

I am read excel and write it to database.

for the duplicate item.

how to get the hash code of each item?

then I can compare it by the hash code.

public class MyClassA
{
public int Id{get;set;}
public string UserInfo{get;set;}
public decimal count{get;set;}
}

how to get the hash of it?

Developer technologies ASP.NET ASP.NET Core
{count} votes

5 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2024-09-07T13:37:42.2466667+00:00

    Try this:

    public class MyClassA
    {
        public int Id { get; set; }
        public string UserInfo { get; set; }
        public decimal count { get; set; }
    
        public override int GetHashCode( )
        {
            return HashCode.Combine( Id, UserInfo, count );
        }
    }
    
    // or this:
    
    public struct MyStructA
    {
        public int Id { get; set; }
        public string UserInfo { get; set; }
        public decimal count { get; set; }
    
        // Use the default 'GetHashCode()'. The hash code is calculated by .NET. 
    }
    

    The documentation recommends to override the Equals(object) function too.

    By the way, if two objects are not equal, the hash codes are not always different. GetHashCode is not enough to find the duplicates.


  2. Viorel 122.5K Reputation points
    2024-09-07T15:08:14.8133333+00:00

    Try an alternative, which is also suggested by Visual Studio wizards:

    public class MyClassA
    {
        public int Id { get; set; }
        public string UserInfo { get; set; }
        public decimal count { get; set; }
    
        public override int GetHashCode( )
        {
            int hashCode = -525015890;
    
            hashCode = hashCode * -1521134295 + Id.GetHashCode( );
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode( UserInfo );
            hashCode = hashCode * -1521134295 + count.GetHashCode( );
    
            return hashCode;
        }
    }
    
    0 comments No comments

  3. mc 5,426 Reputation points
    2024-09-07T15:19:07.5+00:00

    this is not what I want

    0 comments No comments

  4. AgaveJoe 30,126 Reputation points
    2024-09-07T20:14:20.8933333+00:00

    The IEqualityComparer<T> Interface might be solution. The following example gets a distinct list of objects.

    https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=net-8.0

    using System.Diagnostics.CodeAnalysis;
    
    List<MyClassA> items = new List<MyClassA>() 
    { 
        new MyClassA() 
        { 
            Id = 1,
            UserInfo = "User Info 1",
            count = 1
        },
        new MyClassA()
        {
            Id = 2,
            UserInfo = "User Info 2",
            count = 2
        },
        new MyClassA()
        {
            Id = 1,
            UserInfo = "User Info 1",
            count = 1
        },
    };
    
    var DistinctItems = items.Distinct(new MyClassAComparer());
    foreach (var item in DistinctItems)
    {
        Console.WriteLine($"{item.Id} {item.UserInfo} {item.count}");
    }
    
    
    
    public class MyClassAComparer : IEqualityComparer<MyClassA>
    {
        public bool Equals(MyClassA? c1, MyClassA? c2)
        {
            if (ReferenceEquals(c1, c2))
                return true;
    
            if (c1 is null || c2 is null)
                return false;
    
            return c1.Id == c2.Id
                && c1.UserInfo == c2.UserInfo
                && c1.count == c2.count;
        }
    
        public int GetHashCode([DisallowNull] MyClassA v) => v.Id.GetHashCode()
            ^ v.UserInfo.GetHashCode()
            ^ v.count.GetHashCode();
    }
    
    public class MyClassA
    {
        public int Id { get; set; }
        public string UserInfo { get; set; } = string.Empty;
        public decimal count { get; set; }
    }
    

  5. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-09-08T18:26:04.1+00:00

    .net has a built in hash code function (.GetHashCode()) designed for hash table lookups. See

    https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-object-gethashcode

    note: as is true with any hash, two different values may have the same hash, so if you get a hash match, you still need to do a value match.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.