I have been trying the following
interface IUIntegral : IEquatable<Byte>, IEquatable<UInt16>, IEquatable<UInt32>, IEquatable<UInt64> { }
class Counter<T> where T : IUIntegral {
T _value;
}
With this calling code
Counter<UInt32> foo = null;
But I get this compiler error
Error 1 The type 'uint' cannot be used as type parameter 'T' in the generic type or method 'Test.Counter<T>'. There is no boxing conversion from 'uint' to 'Test.IUIntegral'.
tldr; This approach will not work.
C# uses a nominative type system (types are determined by names) and not a structural type system (types determined by data/operations).
unit32andIUIntegralare not related: even if they share the same structure.(They don’t anyway,
uint32does not conform toIEquatable<byte>.)If a type needs to be Equatable with itself this can be done by referring to the type in a type restriction: