Created
June 9, 2013 14:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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