Skip to content

Add C# as another examples #346

@ghost

Description

Hi!
so when I eventually looked at the rust and java code... I realized there is no virtual and override (well it does show in java, but not explicit)

so why not add C# as another examples of polymorphism in appendix B

// C# implementation of the Taylor series example program based on
// subtyping given in Appendix B of the proposal "Improved run-time
// polymorphism for Fortran".
//

using System;

public interface IDeriv {
    void Deriv1();
}

public interface IHDeriv : IDeriv {
    void Deriv2();
}

public abstract class DerivativeBase : IDeriv {
    public virtual void Deriv1() {
        Console.WriteLine(" Default derivative implementation.");
    }
}

public class DerivF : DerivativeBase {
    public override void Deriv1() {
        Console.WriteLine(" 1st derivative of function F!");
    }
}

public class HDerivF : DerivativeBase, IHDeriv {
    public override void Deriv1() {
        Console.WriteLine(" 1st derivative of function F!");
    }

    public void Deriv2() {
        Console.WriteLine(" 2nd derivative of function F!");
    }
}

public class DerivG : DerivativeBase
{
    public override void Deriv1() {
        Console.WriteLine(" 1st derivative of function G!");
    }
}

public class HDerivG : DerivativeBase, IHDeriv {
    public override void Deriv1() {
        Console.WriteLine(" 1st derivative of function G!");
    }

    public void Deriv2() {
        Console.WriteLine(" 2nd derivative of function G!");
    }
}

public class Taylor {
    private readonly IDeriv _calc;

    public Taylor(IDeriv calc) {
        _calc = calc;
    }

    public void Term1() {
        _calc.Deriv1();
    }

    public void Evaluate() {
        Console.WriteLine("Evaluating Taylor series using");
        Term1();
    }
}

public class HTaylor {
    private readonly IHDeriv _calc;

    public HTaylor(IHDeriv calc) {
        _calc = calc;
    }

    public void Term1() {
        _calc.Deriv1();
    }

    public void Term2() {
        _calc.Deriv2();
    }

    public void Evaluate() {
        Console.WriteLine("Evaluating Taylor series using");
        Term1();
        Term2();
    }
}

class Program {
    static void Main() {
        IDeriv derivg = new DerivG();
        IDeriv derivf = new DerivF();
        IHDeriv hderivg = new HDerivG();
        IHDeriv hderivf = new HDerivF();

        Taylor evalG = new Taylor(derivg);
        Taylor evalF = new Taylor(derivf);
        HTaylor hevalG = new HTaylor(hderivg);
        HTaylor hevalF = new HTaylor(hderivf);

        evalG.Evaluate();
        Console.WriteLine();
        evalF.Evaluate();
        Console.WriteLine();
        hevalG.Evaluate();
        Console.WriteLine();
        hevalF.Evaluate();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions