3

In converting .NET Core project to .NET Framework, one thing notice is using Hashcode now needs to be converted something equivalent. As can be read, Hashcode is specific to .NET core versions and some extensions.

https://learn.microsoft.com/en-us/dotnet/api/system.hashcode?view=dotnet-plat-ext-3.1

Wonder what would be close enough in terms of usages in .NET Framework, if there is any in-built object type.

swcraft
  • 2,014
  • 3
  • 22
  • 43

2 Answers2

6

EDIT:

System.HashCode is available in .NET Core too. You can use HashCode.Combine for this purpose, see: https://learn.microsoft.com/en-us/dotnet/api/system.hashcode?view=netcore-2.1

var combined = HashCode.Combine("one", "two");

(previous answer below)

The long and thorough answer(s) can be found here: https://stackoverflow.com/a/34006336/25338

If you just want a simple answer, you can combine the object hashes in a new structure (tuple, anonymous class, etc), and call GetHashCode() on the result.

e.g.

public override int GetHashCode() {
    return new { MyField1, MyField2 }.GetHashCode();
}

Elaborating on the answer linked above, you could of course, if you would like to make it easier (?) create a common static helper class that does the work for you:

 public class MyHashCode
    {
        public static int Hash(params object[] values)
            => CustomHash(1009, 9176, values.Select(v => v.GetHashCode()).ToArray());

        // From answer https://stackoverflow.com/a/34006336/25338
        public static int CustomHash(int seed, int factor, params int[] vals)
        {
            int hash = seed;
            foreach (int i in vals)
            {
                hash = (hash * factor) + i;
            }
            return hash;
        }
    }

and call it like this:

  public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            string s1 = "yezz";
            string s2 = "nope";


            var hash = Utils.MyHashCode.Hash(s1, s2);
        }
    }
Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55
  • one more question, how would you override all GetHashCode() methods previously being called in several different classes in .net core project? Would you just create a HashCode class and do there? – swcraft Mar 31 '20 at 21:36
  • Good question, you could of course that. But how would you call it? Would it be less code that just writing it out? Or would there be any other shared logic in that class that you don't want to duplicate? Do you have a suggestion on what the signature of that class would look like? How would you call it? You could of course create a static `MyHashCode.GetHashCode(params object[] values)`, but how would you dynamically create an anonymous objecct from that? – Erik A. Brandstadmoen Mar 31 '20 at 21:45
  • but otherwise I need to override 10+ class GetHashCode() unless they all inherit from some base class I create having abstract method or something can be overriden. I thought about and didn't like either way. Any better idea is welcome. – swcraft Mar 31 '20 at 21:50
0

No need to reinvent the wheel ‒ on .NET Standard 2.0 (and thus .NET Framework too) you can use the Microsoft.Bcl.HashCode nuget package, which provides the standard System.HashCode implementation for you to use. This is the equivalent (indeed it is more than that) you are looking for.

IS4
  • 11,945
  • 2
  • 47
  • 86