C#/Curiously recurring template pattern
May 27th, 2007 by Andrey
It’s funny, but Curiously recurring template pattern works in C#. Well, works, but it’s not possible to implement static methods calls, because C# interfaces does not support them. Anyway, here is a snippet:
using System;
namespace A {
interface IA {
void Test();
}
class Base<T> where T : class, IA {
public void ImplTest() {
(this as T).Test();
}
}
class A : Base<A>, IA {
public void Test() {
Console.WriteLine(“A.Test()”);
}
}
public class _ {
public static void Main(string[] args) {
new A().ImplTest();
}
}
}
you should have declared the base class as:
class Base where T : Base, IA
{…}
confusing at first look:)
It’s impossible from the language point of view.
error CS0305: Using the generic type ‘A.Base‘ requires ‘1′ type arguments