Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C# as another examples #346

Open
Phyllee opened this issue Dec 11, 2024 · 0 comments
Open

Add C# as another examples #346

Phyllee opened this issue Dec 11, 2024 · 0 comments

Comments

@Phyllee
Copy link

Phyllee commented Dec 11, 2024

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();
    }
}
@Phyllee Phyllee changed the title Add C# as another example. Add C# as another examples Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant