The missing link in C# generics: TThis

I was talking to a friend of mine (Mike Hurley, who doesn’t have a blog so I can’t properly cite him) and he had an interesting solution to a problem in C# that has nagged me for a while. The problem is with strongly typing generic members to that of a sub-class. Let me explain.

The example he gave me was with ICloneable. ICloneable has a single method Clone, which returns an object. Rather than casting everywhere he decided to take the next logical step, which was to make a generic IClonable.

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

Now to follow this logic you might want to create a ClonableBase class that either provides virtual methods to implement the cloning or has a generic scheme (such as serialization / deserialization) to do the cloning automatically for you.

public abstract class CloneableBase<T> : ICloneable<T>
    where T : ICloneable<T>
{
    object ICloneable.Clone()
    {
        return this.Clone();
    }

    public T Clone()
    {
        T clone = default(T);
        // do clone...
        return clone;
    }
}

The only problem with this is that you have to know what T is. In this case, T is the subclass of this. But there is no way to express TThis in C#. The best you can do is to have the recursive generics like above which when implemented would look like this.

public class Example : CloneableBase<Example>
{
}

While this works just fine in practice it has a few drawbacks. The first is just that it sort of makes your brain hurt a little bit and feels a little wrong. Another reason is that it makes performing reflection and casting much more difficult (though I suppose you could always down cast it into IClonable in this example). And finally it doesn’t actually express the constraint the base class is expecting, meaning you can write invalid code. Consider this:

public class Example : CloneableBase<Foo>
{
}

This would build just as easily but is clearly, totally wrong. There is no way in C# to properly express this constraint correctly. Not yet at least. Mike was recommending adding a TThis feature to the language. The corresponding code with this feature might look like this.

public abstract class CloneableBase : ICloneable<TThis>
{
    object ICloneable.Clone()
    {
        return this.Clone();
    }

    public TThis Clone()
    {
        TThis clone = default(TThis);
        // do clone...
        return clone;
    }
}

public class Example : CloneableBase
{
}

Where TThis is literally the type of the instance, or the Type about to be instantiated. I’m not sure how you would retrofit this to the current generics system but it seems plausible. If you’re not sure how common or useful this feature would be take a look at CSLA and see BusinessBase<T> among others.

Author: justinmchase

I'm a Software Developer from Minnesota.

Leave a Reply

%d bloggers like this: