简体   繁体   English

GetHashCode 方法 asp.net 核心中相同字符串返回不同值

[英]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.它在“asp.net 核心”和“标准”框架中有不同的行为。

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. Framework 4.7:如果我将在 .net 标准框架中创建的网站/控制台应用程序中使用此代码,它总是会给我相同的结果。 The value of hashcode variable is always "1066337973". hashcode 变量的值始终为“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. Asp.net 核心:如果我将在 asp.net 核心网站中使用此代码,那么每次我停止应用程序并重新启动时,哈希码变量都有不同的值。

Becasue of this problem, my logic is getting failed in asp.net core application.由于这个问题,我的逻辑在 asp.net 核心应用程序中失败了。
How this problem can be solved?如何解决这个问题?

As per @Pavel Anikhouski根据@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? GetHashCode实现在 .NET Core 中发生了变化,这是预期的行为,请查看这篇文章为什么每次在 .NET Core 中运行我的程序时 string.GetHashCode() 都不同?

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. String.GetHashCode Hash 相同字符串的代码在 .NET 实现、.NET 版本和 .NET 平台(例如 32 位和 64 位)中对于 .NET 的单个版本可能不同。在某些情况下,它们甚至可能因应用程序而异领域。 This implies that two subsequent runs of the same program may return different hash codes.这意味着同一程序的两次后续运行可能会返回不同的 hash 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM