0

Moving from Java to C# developer. In Java, I used a lot of Objects.hash(array) and Objects.hashCode(object) to build hash code of an object in hashCode function. I can't find any equivalent for those functions in C#. Any ideas?

Indeed I can call GetHashCode and combine them as shown in Concise way to combine field hashcodes?, but that requires a lot of null checks for reference types and that is making code much longer than comparable Java code.

In Java:

public class MyObject {

    private Integer type;
    private String[] attrs;
    
    @Override
    public int hashCode() {
        int hash = 1, p = 31;
        hash = p * hash + Objects.hashCode(type); // Handle Null case
        hash = p * hash + Objects.hash(attrs); // Handle Null case
        return hash;
    }
}

In C#:

public class MyObject {

    private int? type;
    private string[] attrs;
    
    public override int GetHashCode()
        int hash = 1, p = 31;
        hash = p * hash + hash_of(type);    // Any utilities?
        hash = p * hash + hash_of_array(attrs); // Any utilities?
        return hash;
    }
}

Note: I'm not looking for compatible results (as I know that hash codes would be different for similar objects, and can be different even between versions/platform for the same framework) but rather code size & concise way.

16
  • Every object in .Net implements GetHashCode. Are you asking for something else? Commented Sep 18, 2019 at 17:46
  • @Amy: GetHashCode only came with default implementation. We still need to override it if we want use the object as map key or put the object into a hash set. See this: learn.microsoft.com/en-us/dotnet/api/… Commented Sep 18, 2019 at 17:48
  • What "correct" implementation? .Net hash codes are not going to be the same as Java hash codes. They are used for hash sets all the time without issue. Commented Sep 18, 2019 at 17:50
  • Why not the same? You are understanding wrong about object hash code and why we need it... Commented Sep 18, 2019 at 17:54
  • You're right, I don't understand why the default implementation works for everyone else, but not for you. Commented Sep 18, 2019 at 17:54

1 Answer 1

5

The .NET equivalent of this is the new System.HashCode, in particular the HashCode.Combine<> method which you can use to create a hash code from multiple values:

public class MyObject
{
    private int? type;
    private string[] attrs;

    public override int GetHashCode()
        => HashCode.Combine(type, attrs);

    // remember to also override Equals
}

Note that this type is only available in .NET Core. If you are running on .NET Framework, you will have to implement the hash code calculation yourself. Good ways to do that are documented here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for System.HashCode.Combine, that was exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.