Skip to content

Instantly share code, notes, and snippets.

@parsalotfy
Created October 8, 2019 13:07
Show Gist options
  • Save parsalotfy/3209ef2e71255773342b99cab9700f32 to your computer and use it in GitHub Desktop.
Save parsalotfy/3209ef2e71255773342b99cab9700f32 to your computer and use it in GitHub Desktop.
A generic equality comparer for type of T.
using System;
using System.Collections.Generic;
namespace SomeNameSpace
{
public class TEqualityComparer<T> : EqualityComparer<T>
{
private TEqualityComparer()
{
}
public TEqualityComparer(Func<T,T,bool> equalsMethod, Func<T, int> getHashCodeMethod)
{
EqualsMethod=equalsMethod;
GetHashCodeMethod=getHashCodeMethod;
}
public Func<T,T,bool> EqualsMethod { get; }
public Func<T, int> GetHashCodeMethod { get; }
public override bool Equals(T x, T y)
{
return EqualsMethod(x,y);
}
public override int GetHashCode(T t)
{
return GetHashCodeMethod(t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment