Sunday 8 January 2012

Article 3:-Advance Detail of constructor


For Asp.net Training with C# Click Here

Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com


In the previous article we have learnt the class and constructor in the basic level and in second article we learnt constructor in more detail. Now In this article we will learn advance detail about the constructors
Calling Constructor from another Constructor
Or
Constructor chaining
If you ask the question that can we call constructor from another constructor then I will say yes we can. Calling constructor from another constructor is known as constructor chaining. This can be done by the keyword this and base. Here we discuss on this keyword and will discuss base keyword in the article constructor with inheritance.
Example of constructor chaining
Public class class1
{
            Public class1:this(10)
            {
            }
            Public class1 (int x)
            {
            }
}
In this way constructor will call another constructor.
Note:-static constructor cannot be chained. And a public constructor can access a private constructor with the help of constructor chaining.

No comments:

Post a Comment