vendredi 10 avril 2020

How to force the derived class to override a method that returns its class name only?

I don't know what pattern I have to implement for forcing the derived class to override the method Print returning only its class name.

class Base
{
    protected virtual string Print() => nameof(Base);

    static void Main(string[] args)
    {
        Base b = new Derived();
        Console.WriteLine(b.Print());
    }
}

class Derived : Base
{
    protected override string Print() => nameof(Derived);
}

Aucun commentaire:

Enregistrer un commentaire