Created
June 28, 2013 16:45
-
-
Save iamrommel/5886129 to your computer and use it in GitHub Desktop.
Generic Equality comparer that can be used of linq
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
using System; | |
using System.Collections.Generic; | |
namespace LightSwitchApplication | |
{ | |
/// <summary> | |
/// Generic Equality comparer that can be used of linq | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
public class EqualityComparer<T> : IEqualityComparer<T> | |
{ | |
private readonly Func<T, T, bool> _equalsFunction; | |
private readonly Func<T, int> _getHashCodeFunction; | |
public EqualityComparer(Func<T, T, bool> equalsFunction) | |
{ | |
_equalsFunction = equalsFunction; | |
} | |
public EqualityComparer(Func<T, T, bool> equalsFunction, | |
Func<T, int> getHashCodeFunction) | |
: this(equalsFunction) | |
{ | |
_getHashCodeFunction = getHashCodeFunction; | |
} | |
public bool Equals(T a, T b) | |
{ | |
return _equalsFunction(a, b); | |
} | |
public int GetHashCode(T obj) | |
{ | |
if (_getHashCodeFunction == null) | |
return obj.GetHashCode(); | |
return _getHashCodeFunction(obj); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment