简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM