Weird use of generics

Posted by Karl Trumstedt on Stack Overflow See other posts from Stack Overflow or by Karl Trumstedt
Published on 2010-05-25T08:43:58Z Indexed on 2010/05/25 8:51 UTC
Read the original article Hit count: 311

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics