Search code examples
c#hashcodegethashcode

How to use System.HashCode.Combine with more than 8 values?


.NET Standard 2.1 / .NET Core 3 introduce System.HashCode to quickly combine fields and values to a hash code without having to care about the underlying implementation.

However, it only provides Combine method overloads for up to 8 values. What do I do if I have a class with 9 values (3x3 matrix) or even 16 values (4x4 matrix)?

Should I simply add together the results of two Combine calls, passing as many values as possible in each?

public override int GetHashCode()
    => HashCode.Combine(M11, M12, M13, M21, M22, M23, M31, M32) + HashCode.Combine(M33);

Looking at the source, I cannot completely argue if this may have implications I don't know of.


Solution

  • As stated in the System.HashCode documentation, adding together hashes returned by successive HashCode.Combine calls is NOT the solution.

    While the static HashCode.Combine method overloads only allow up to 8 values, these are just convenience methods - to combine more, instantiate the HashCode class itself and use it as follows:

    public override int GetHashCode()
    {
        HashCode hash = new();
        hash.Add(M11);
        hash.Add(M12);
        hash.Add(M13);
        hash.Add(M21);
        hash.Add(M22);
        hash.Add(M23);
        hash.Add(M31);
        hash.Add(M32);
        hash.Add(M33);
        return hash.ToHashCode();
    }
    

    It does make me wonder why there is no HashCode constructor accepting a params object[] values so you could do all that in one line, but there are probably reasons I didn't think of this quickly. (S. the comments why such an overload does not exist.)