Consider the following generic:
class Foo<T, U> {}
Is it possible to prevent T and U from being the same type? This question is borne out of curiosity.
No it's not. The only options you have are listed here Constraints on Type Parameters
http://msdn.microsoft.com/en-us/library/d5x73970%28v=vs.80%29.aspx shows us that there are 6 kinds of constraint. Equality is not included.
As pointed out by others, you can not use Type Constraints but you can still add runtime checks using static constructors. See http://en.csharp-online.net/ECMA-334:_25.1.5_Static_constructors_in_generic_classes
No, there isn't such a thing. But you might want to look into other languages with template metaprogramming, such as D -- they are much more powerful, compile-time variants of generics.
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.