ICloneable

We switched over to .NET 2.0 recently. I really dig on the new features: generics, anonymous methods, etc…

One thing caught my eye today, however: while there are many new generic-ized classes and interfaces (List < T > , IComparable < T > ) our good ol’ friend ICloneable does not have a generic counterpart. It brought me up short. Why not?

The closest thing I can come up with is this hypothetical:

public interface ICloneable < T >
{
   T Clone();
}
	

public class Base : ICloneable < Base >
{ Base Clone() { ... }
}

public class Derived : Base
{ Base Clone() { ... }
}

Now, suppose someone wanted to stick a ICloneable < Derived > on the Derived class. Well, they couldn’t: since method overloading only uses the signature of the method (which doesn’t include the return type, only the name of the method and the parameters of the method) there would be no difference between the ICloneable < Base > Clone method and the ICloneable < Derived > Clone method. The user would have to implement two separate methods like this: ICloneable < Base > .Clone and ICloneable < derived > .Clone, which seems somehow clunky. Imagine a DerivedDerived type, saddled with another of these methods, and on and on…

I’m pretty sure of the six people who read my blog three are coders… what do you think? Does that reasoning make sense? Is this a new C# interview question?

P.S. & lt; and & gt; really suck for typing generic interfaces.
P.P.S. Moving sucks too, but, y’know, worse.

3 Responses to “ICloneable

  1. chris k Says:

    hey david,
    i changed computers and i killed my cell phone, so i’ve now lost all contact info for you and terra. could you help me out? i’ve been meaning to call and say hi, but i’m presently technologically isolated…

    how are things?

    chris

  2. sean Says:

    Cloneable is evil.

    Generics are powerful.

    Stop trying to combine them.

    :)

  3. David Says:

    Yeah, I know Cloneable is generally evil…

    Have you read “Effective C#”? Holy crap it’s a good book. (BTW… “Item 27: Avoid ICloneable”)