Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impossible recursive generic class definition?

Tags:

c#

Challenge: Please create an instance of the following class (using any type as T):

class Foo<T>
    where T : Foo<T>
{
}

You may use any technique you like; plain "new MyClass...", using reflection, hacking MSIL, whatever.

like image 801
Mattias Larsson Avatar asked Feb 03 '23 07:02

Mattias Larsson


1 Answers

static class Program {
    static void Main() {
        Foo<Bar> foo = new Foo<Bar>();
    }
}
class Foo<T> where T : Foo<T> {}
class Bar : Foo<Bar> {}
like image 90
Marc Gravell Avatar answered Feb 04 '23 21:02

Marc Gravell