Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            var der1 = new Derived1();
            var der2 = new Derived2();

            Console.WriteLine("Derived 1: {0}", der1.MyMethod());
            Console.WriteLine("Derived 2: {0}", der2.MyMethod());

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Base2 b = new Base2();
            b.Execute();
            b = new Derived2();
            b.Execute();

            // Running this code will output Base2.Execute twice.
            // If you change the base execute method to be virtual and
            // the derived class to override instead of hide the Execute method,
            // the code will display Base2.Execute and Derived2.Execute.
            // You should try to avoid hiding methods with the new keyword.

            Derived2 d = new Derived2();
            d.Execute();
            b = d;
            b.Execute();

            Console.ReadLine();
        }