Search code examples
c#genericsiequalitycomparer

c# implementing IEqualityComparer<T> for generic class T


Is there any way of implementing IEqualityComparer for generic class?

I tried:

public class MyComparer : IEqualityComparer<MyGenericClass>

which is wrong, because MyGenericClass takes 3 arguments as a generics, so next one

public class MyComparer : IEqualityComparer<MyGenericClass<A, B, C>>

which is wrong, because I don't know types A,B,C. And so

public class MyComparer<MyGenericClass<A, B, C>> : IEqualityComparer<MyGenericClass<A, B, C>>

is wrong. Is there a way of implementing this? Thanks


Solution

  • You are close

    public class MyComparer<A, B, C> : IEqualityComparer<MyGenericClass<A, B, C>>
    

    Side note, please don't name things A, B, and C. That doesn't help anyone. Standard naming convention is T<SomethingDescriptive>. In the case of EF, it might be TEntity, in MVC or MVVM, it might be TModel or TViewModel.