C# – Generic class that inherits its generic type


Possible Duplicates:
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?
In C# 4.0, is it possible to derive a class from a generic type parameter?

I'm trying to create a generic class that inherits from its generic type (in 1.Attempt). Looks like it's impossible. But in other attempt i must cast the object. Any design solution to that problem?

// #1 Attempt
public interface IFoo { }

public class Generic<T> : T { }

public void play ()
    IFoo genfoo = new Generic<IFoo> ();


// #2 Attempt. Castable solution
public interface IAnything { }

public interface IFoo2 : IAnything { }

public class Generic2<T> : IAnything { }

public void play2 ()
    IFoo2 genFoo = (IFoo2) new Generic2<IFoo2> ();

Best Solution

I don't see the point of the code you provided in Attempt #1. Why would you inherit a class and then pass that same class as generic param.

Nevertheless, generic inheritance is supported but its done differently, please check this link, it might give you an idea: Generic Inheritance

Hope this helps.