// Copy constructor. public CopyConstructorExampleClass(CopyConstructorExampleClass previousCtor) { Name = previousCtor.Name; }
static void Main(string[] args) { #region Default constructors & Instance constructors //BaseClass dc = new BaseClass(); //Console.WriteLine("Defualt Constructors initialized = {0}", dc.isInitialized); //Console.WriteLine("The value of sum dc= {0}", dc.sum); //BaseClass dc2 = new BaseClass(10, 20); //Console.WriteLine("The value of sum dc2= {0}", dc2.sum); //ChildClass childObj = new ChildClass(); //Console.WriteLine("Defualt Constructors initialized = {0}", childObj.isInitialized); //ChildClass childObj2 = new ChildClass(10, 20); //Console.WriteLine("The value of sum childObj2= {0}", childObj2.sum); //ChildClass childObj3 = new ChildClass(10, 20); //Console.WriteLine("Defualt Constructor from base class initialized = {0}", childObj3.isInitialized); //// Keep the console window open in debug mode. //Console.WriteLine("Press any key to exit."); //Console.ReadKey(); #endregion #region Private Constructors ////PrivateConstructorExampleClass Obj = new PrivateConstructorExampleClass(); //Error //PrivateConstructorExampleClass.age = 10; //Console.WriteLine("Age static variable value assigned is = {0}", PrivateConstructorExampleClass.age); //// Keep the console window open in debug mode. //Console.WriteLine("Press any key to exit."); //Console.ReadKey(); #endregion #region Static Constructors //// StaticConstructorExampleClass Obj = new StaticConstructorExampleClass(); //Error //StaticConstructorExampleClass Obj1 = new StaticConstructorExampleClass(10, 20); //StaticConstructorExampleClass Obj2 = new StaticConstructorExampleClass(10, 20, 30); //// Keep the console window open in debug mode. //Console.WriteLine("Press any key to exit."); //Console.ReadKey(); #endregion #region Copy Constructors // Create a Person object by using the instance constructor. CopyConstructorExampleClass Obj = new CopyConstructorExampleClass("Divakar"); // Create another Person object, copying person1. CopyConstructorExampleClass Obj2 = new CopyConstructorExampleClass(Obj); Obj.Name = "Alex"; Console.WriteLine("Name from instance constructor is {0}", Obj.Name); Console.WriteLine("Name from copy constructor is {0}", Obj2.Name); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); #endregion }