Skip to content

Instantly share code, notes, and snippets.

@kcargile
Created September 14, 2012 18:39
Show Gist options
  • Save kcargile/3723840 to your computer and use it in GitHub Desktop.
Save kcargile/3723840 to your computer and use it in GitHub Desktop.
.NET Determines if the two objects are equivalent in a way that will not throw if either is null.
namespace Extensions
{
/// <summary>
/// Contains <see cref="object"/> extension methods.
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// Determines if the two objects are equivalent in a way that will not throw if either is null.
/// </summary>
/// <param name="param">The param.</param>
/// <param name="obj">The obj.</param>
/// <returns><c>true</c> if the two objects are equivalent; otherwise, <c>false</c>.</returns>
public static bool NullSafeEquals(this object param, object obj)
{
return null != param ? param.Equals(obj) : (null == obj ? true : false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment