Constructors are class methods which executes when class object is created.
Constructors can be marked as public, private, protected, internal, protected internal, static.
Classes can have multiple constructors. If we do not specify constructor, c# will create default one.
Scenario 1: Inheritance and default constructors
When we inherit base class (Base1) in child class (Base2), and create an object of child class, it executes base class constructor first and child class constructor later. This helps that when child class constructor is executing, system makes sure that base class has already set properties or initializations and then child class can re-set those according to need. Refer below example for this:
class Base1
{
public Base1()
{ Console.WriteLine("Base 1 .ctor called");
}
}
{
public Base1()
{ Console.WriteLine("Base 1 .ctor called");
}
}
class Base2 : Base1
{public Base2()
{ Console.WriteLine("Base 2 .ctor called");
}
}
Actual call is
Base2 base2Obj = new Base2(); or
Base1 base1Obj = new Base2();
Output is
Base 1 .ctor called
Base 2 .ctor called
Even if you write base2 constructor as below, output will be the same as mentioned above.
public Base2() : base()
{
Console.WriteLine("Base 2 .ctor called");
}
Conclusion:
It always the base class costructor that get called first. Derived class may not like the way that the base class has been initialized. It can change the initial value of data provided that it has access to do so.Scenario 2: Constructors with Parameters
class Base1
{
private string _name;
public Base1(string name)
{ Console.WriteLine("Base 1 .ctor called");
_name = name;
}
}
class Base2 : Base1
{public Base2(string name): base(name)
{ Console.WriteLine("Base 2 .ctor called");
}
}
In above example, base2 class constructor does not have access to private variable of base1 class. However by calling base class .ctor (constructor), it can initialize base class variables too.
Hence output is:
Base 1 .ctor calledBase 2 .ctor called
In above example, if Base2 .ctor is not calling base class constructor (:base(name) call),
then program generates compile time error that there is no constructor that takes 0 arguments in Base1 class. If child class is not calling parent class parameterized constructor explicitly then default constructor has to be present in parent class. Again it executes first and then of child class when you are creating child class object.
Scenario 3: Static Constructor
If just Base1 class is having static constructor, then whenever object of Base2 is created, it executes Base1 static constructor first before default Base1 .ctor and then default Base2 .ctor.
If Base1 and Base2 both are having static .ctor, then execution way is as below when Base2 object is created.
- Base 2 static .ctor
- Base 1 static .ctor
- Base1 default .ctor
- Base2 default .ctor
class Base1
{
static Base1()
{
//do something
}
public Base1()
{ Console.WriteLine("Base 1 .ctor called");
}
}
{
static Base1()
{
//do something
}
public Base1()
{ Console.WriteLine("Base 1 .ctor called");
}
}
class Base2 : Base1
{static Base2()
{
//do something
}
public Base2()
{ Console.WriteLine("Base 2 .ctor called");
}
}
As we know, A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
Scenario 4: Private constructor
It is generally used in the classes that contains static members only. If class is just having one or more private constructor, other classes cannot create class's instance. Class needs to have other than private constructor to let other classes to create its instance.If class is not having static members and will contain private constructor then .net produces compile time error as, XYZ is inaccessible due to its protection level.
Scenario 5: Writing a copy constructor
Unlike other languages, c# does not provide copy constructor facility. You need to write appropriate method to copy values from existing object.
MSDN has very well explained how to write method to copy values of existing object to current new object. http://msdn.microsoft.com/en-us/library/ms173116
Good coding Practice
- Each constructor handles initialization of the variables that are obviously its responsibility. By following this, you can find that complex classes get initializes simply and code will be maintainable and readable.
No comments:
Post a Comment