Exemple #1
0
        // Ex 4: Use new keyword to prevent polymorphism
        public void Ex4_New()
        {
            ParentA a = new ChildB();

            a.Method02(); // calls childA.method2

            ParentA b = new ChildB();

            b.Method02(); // calls childA.method2
        }
Exemple #2
0
        // Ex 3: Use virtual/override
        public void Ex3_Virtual()
        {
            ChildA a = new ChildA("hello");

            a.Method01(); // no override
            a.Method02(); // override
            a.Prop01 = "test";
            Console.WriteLine("Prop01: {0}", a.Prop01);

            ChildA b = new ChildB();

            b.Method03(); // Sealed
            b[0] = "Hello world";
            Console.WriteLine("Indexer: {0}", b[0]);
            b.OnChangeCustom += (o, e) => { Console.WriteLine("Test override"); };
        }