class MyClass<T> where T : IEquatable<T> { }
and an enum:
enum MyEnum { MyOption1, MyOption2, MyOption3 }
would you be able to do this?
MyClass<MyEnum> MyMember;
Nope:
The type 'MyEnum' cannot be used as type parameter 'T' in the generic type or method 'MyClass<T>'. There is no boxing conversion from 'MyEnum' to 'System.IEquatable
What would you do?
Use EqualityComparer<T>.Default. By definition, behaviour is:
The Default property checks whether type T implements the System.IEquatable<T> generic interface and if so returns an EqualityComparer<T> that uses that implementation. Otherwise it returns an EqualityComparer<T> that uses the overrides of Object.Equals and Object.GetHashCode provided by T.
Nice.
No comments:
Post a Comment