using System; namespace Sample { public class Base { protected string initialized = "No"; public Base() { Console.WriteLine( "Calling base ctor." ); DoSomething(); } // This will be overridden in the derived type. public virtual void DoSomething() { Console.WriteLine( "Base DoSomething" ); } } public class DerivedType : Base { public DerivedType () { Console.WriteLine( "Calling derived ctor." ); initialized = "Yes"; } public override void DoSomething() { Console.WriteLine( "Derived DoSomething is called - initialized ? {0}", initialized ); } } class Program { static void Main(string[] args) { Base derivedInstance = new DerivedType(); } } } Output: Calling base ctor. Derived DoSomething is called - initialized ? No Calling derived ctor.
Is there something wrong ? … Yes of course Read the rest of this entry »