Yesterday, I was explaining C#'s generic constraints to my friends. When demonstrating the where T : CLASSNAME
constraint, I whipped up something like this:
public class UnusableClass<T> where T : UnusableClass<T>
{
public static int method(T input){
return 0;
}
}
And was really surprised to see it compile. After a bit of thinking, however, I figured it was perfectly legal from the point of view of the compiler - UnusableClass<T>
is as much of a class as any other that can be used in this constraint.
However, that leaves a couple of questions: how can this class ever be used? Is it possible to
- Instantiate it?
- Inherit from it?
- Call its static method
int method
?
And, if yes, how?
If any of these is possible, what would the type of T
be?