I'm trying to implement Curiously recurring template pattern(CRTP)
in c#.
here is some code i wrote.
using System;
using System.Collections;
using System.Collections.Generic;
// Curiously recurring template pattern in c#
namespace MyApp
{
public class Program
{
public static void Main (string[] arg)
{
new Child().CallChildMethod();
}
}
public abstract class Base <T> where T: Base<T>, new ()
{
// Game loop
void Upate ()
{
Method ();
}
public void CallChildMethod ()
{
T t = (T)this;
t?.Method ();
}
public void Method ()
{
Console.WriteLine ("Base Method!");
}
}
public class Child: Base <Child>
{
public new void Method ()
{
Console.WriteLine ("Child Method!");
}
}
}
In output i'm getting
Base Method!
but my code should print
Child Method!
any idea?
Expected
I want to access child
class object in base
class instead of overriding
base methods and please suggest is there any other way to do the same?.