Skip to content

Instantly share code, notes, and snippets.

@willnationsdev
Created January 30, 2026 21:31
Show Gist options
  • Select an option

  • Save willnationsdev/8fbb043347d9890417e80c23c553a1e4 to your computer and use it in GitHub Desktop.

Select an option

Save willnationsdev/8fbb043347d9890417e80c23c553a1e4 to your computer and use it in GitHub Desktop.
A barebones HashCode type for use in source generators and other dependency-free contexts.
/// <summary>
/// Minimal HashCode helper for netstandard2.0.
/// Intended for use in source generators and other dependency-free contexts.
/// </summary>
internal struct HashCode
{
private const int Seed = unchecked((int)2166136261);
private const int Factor = 16777619;
private int _hash;
public void Add<T>(T? value)
{
Add(value?.GetHashCode() ?? 0);
}
public void Add(int value)
{
unchecked
{
_hash = (_hash == 0 ? Seed : _hash);
_hash = (_hash * Factor) ^ value;
}
}
public int ToHashCode()
{
return _hash;
}
public static int Combine<T1>(T1? value1)
{
var hc = new HashCode();
hc.Add(value1);
return hc.ToHashCode();
}
public static int Combine<T1, T2>(T1? value1, T2? value2)
{
var hc = new HashCode();
hc.Add(value1);
hc.Add(value2);
return hc.ToHashCode();
}
public static int Combine<T1, T2, T3>(T1? value1, T2? value2, T3? value3)
{
var hc = new HashCode();
hc.Add(value1);
hc.Add(value2);
hc.Add(value3);
return hc.ToHashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment