Weird use of generics
- by Karl Trumstedt
After a bit of programming one of my classes used generics in a way I never seen before. I would like some opinions of this, if it's bad coding or not.
abstract class Base<T> : where T : Base<T>
{
// omitted methods and properties.
virtual void CopyTo(T instance) { /*code*/ }
}
class Derived : Base<Derived>
{
override void CopyTo(Derived instance)
{
base.CopyTo(instance);
// copy remaining stuff here
}
}
is this an OK use of generics or not? I'm mostly thinking about the constraint to "itself". I sometimes feel like generics can "explode" to other classes where I use the Base class.