Question: What is the output of below program?

class A
    {
        public virtual void Function()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public override void Function()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public new void Function()
        {
            Console.WriteLine("C");
        }
    }

    public class Example
    {
        public static void Main()
        {
            A a = new C();
            a.Function();
            Console.ReadLine();
        }


    }

Answer: It will print "B", as class B override the method of class A.

Comments

Popular posts from this blog

iframe vs embed vs object in HTML 5

Constructor in c#

What is the need of method overriding ???