visit
I searched the internet and found that this is true. According to
The hash code itself is not guaranteed to be stable. 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.
As a result, hash codes should never be used outside of the application domain in which they were created, they should never be used as key fields in a collection, and they should never be persisted.
Finally, don’t use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the
or class.
For more information about hash codes, see
.
class Program
{
static void Main(string[] args)
{
var employee = new Employee("Ahmed");
Console.WriteLine(employee.GetHashCode());
Console.ReadLine();
}
}
public sealed class Employee : IEquatable<Employee>
{
public string Name { get; }
public Employee(string name)
{
Name = name;
}
public override int GetHashCode()
{
return (Name != null ? Name.GetHashCode() : 0);
}
public bool Equals(Employee other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Employee)obj);
}
public static bool operator ==(Employee left, Employee right)
{
return Equals(left, right);
}
public static bool operator !=(Employee left, Employee right)
{
return !Equals(left, right);
}
}
A simple class Employee
with a simple implementation of IEquatable<Employee>
interface.
Just notice how the GetHashCode
method is implemented.
using System.Collections.Generic;
namespace StringGetHashCode
{
public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]>
{
private static readonly EqualityComparer<T> ElementComparer
= EqualityComparer<T>.Default;
public static ArrayEqualityComparer<T> Default => new();
public bool Equals(T[] x, T[] y)
{
if (x == y)
{
return true;
}
if (x == null || y == null)
{
return false;
}
if (x.Length != y.Length)
{
return false;
}
for (var i = 0; i < x.Length; i++)
{
if (!ElementComparer.Equals(x[i], y[i]))
{
return false;
}
}
return true;
}
public int GetHashCode(T[] obj)
{
unchecked
{
var hash = 17;
foreach (var element in obj)
{
hash = hash * 31 + ElementComparer.GetHashCode(element);
}
return hash;
}
}
}
}
using System.Security.Cryptography;
using System.Text;
namespace StringGetHashCode
{
public static class StringExtensions
{
public static int GetReliableHashCode(
this string str,
HashAlgorithm hashingAlgorithm) =>
ArrayEqualityComparer<byte>.Default.GetHashCode(
hashingAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(str)));
}
}
class Program
{
static void Main(string[] args)
{
var fixedEmployee = new FixedEmployee("Ahmed");
Console.WriteLine(fixedEmployee.GetHashCode());
Console.ReadLine();
}
}
public sealed class FixedEmployee : IEquatable<FixedEmployee>
{
public string Name { get; }
public FixedEmployee(string name)
{
Name = name;
}
public override int GetHashCode()
{
var hashCode = -1587353937;
using (var ha = new System.Security.Cryptography.SHA1Managed())
{
hashCode = hashCode * -1521134295 + Name.GetReliableHashCode(ha);
}
return hashCode;
}
public bool Equals(FixedEmployee other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((FixedEmployee)obj);
}
public static bool operator ==(FixedEmployee left, FixedEmployee right)
{
return Equals(left, right);
}
public static bool operator !=(FixedEmployee left, FixedEmployee right)
{
return !Equals(left, right);
}
}
If you need the result of String.GetHashCode()
to be persistent between your application run sessions, you will need to follow this way or another similar way. Otherwise, you will end up with a new result every time you stop and run the application.