Gurpreet Kailey
Gurpreet Kailey

Reputation: 669

GetHashCode method Returns different values for same string in asp.net core

I have following code written in application.

  var salt = "0115ee62e6e5e8ea68db9e2d7e72cec85e810ad9566212ffc2c4b76a17ca0b00";
  var hashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(salt ?? string.Empty);

It has different behaviour in 'asp.net core' and 'standard' framework.

Framework 4.7: If I will use this code in website/console application created in standard framework of .net, it alwasy give me same result. The value of hashcode variable is always "1066337973".

Asp.net Core: If I will use this code in asp.net core website, then every time I stop the application and start again, the hashcode variable have differnt value.

Becasue of this problem, my logic is getting failed in asp.net core application.
How this problem can be solved?

Upvotes: 3

Views: 1528

Answers (1)

Gurpreet Kailey
Gurpreet Kailey

Reputation: 669

As per @Pavel Anikhouski

GetHashCode implementation was changed in .NET Core, it's expected behavior, have a look at this article Why is string.GetHashCode() different each time I run my program in .NET Core?

String.GetHashCode Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes.

Upvotes: 6

Related Questions