Skip to content

Instantly share code, notes, and snippets.

@kcargile
Created June 9, 2013 14:28
Show Gist options
  • Save kcargile/5743742 to your computer and use it in GitHub Desktop.
Save kcargile/5743742 to your computer and use it in GitHub Desktop.
Calculates a hash for an object in such a way that will not throw of affect the value if the object is null.
namespace Extensions
{
/// <summary>
/// Contains <see cref="object"/> extension methods.
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// Calculates a hash for the object in such a way that will not throw or affect the value if the object is null.
/// </summary>
/// <param name="param">The param.</param>
/// <param name="seed">The seed.</param>
/// <returns>Hash.</returns>
public static int NullSafeHash(this object param, int seed)
{
return null != param ? seed*7 + param.GetHashCode() : seed;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment