// dynamic and static dispatch in C# using System; public interface I { void f1(); } class A : I { public void f1() {Console.WriteLine("A.f1");} // static overloading: public void h(B x) {Console.WriteLine("h(B) called");} public void h(B2 x) {Console.WriteLine("h(B2) called");} } class B : I { public void f1() {Console.WriteLine("B.f1");} public virtual void g1() {Console.WriteLine("B.g1");} } class B2 : B,I { public new void f1() {Console.WriteLine("B2.f1");} public override void g1() {Console.WriteLine("B2.g1");} } public class dispatch { public static void Main() { I m = new B2(); m.f1(); // DYNAMIC DISPATCH implicit in interface methods B n = new B2(); // account n = new savingsaccount() ... n.f1(); // uses STATIC type to dispatch here n.g1(); // uses DYNAMIC DISPATCH here because of virtual-override (new A()).h(n); // uses static resolution (B version) // (new A()).h(m); // compiler error, why? }//main }