Skip to content

Instantly share code, notes, and snippets.

@iamrommel
Created June 28, 2013 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iamrommel/5886129 to your computer and use it in GitHub Desktop.
Save iamrommel/5886129 to your computer and use it in GitHub Desktop.
Generic Equality comparer that can be used of linq
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