Subscribe to
Posts
Comments

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();
     }
 }
 }

2 Responses to “C#/Curiously recurring template pattern”

  1. on 01 Feb 2008 at 8:21 pmcontrapunctus

    you should have declared the base class as:

    class Base where T : Base, IA
    {…}

    confusing at first look:)

  2. on 01 Feb 2008 at 9:38 pmAndrey

    It’s impossible from the language point of view.

    error CS0305: Using the generic type ‘A.Base‘ requires ‘1′ type arguments

Leave a Reply