Constructor Chaining in C#

Dear All,

I am actually new to Object Orientated Programming. I was just studing about Constructors  and I came across the concept of Constructor Chaining.Can some 1
explain me about what is this constructor chaining.I know it means that we can call one constructor from another constructor. I have tried 1 example which is
as follows:


class Class1

{

private string e_name,e_JobTiltle,e_Address ;

private Class1(string name,string Title)

{
e_name =name;
e_JobTiltle=Title;
}

private Class1(string name,string Title,string Address)

{
e_name =name;
e_JobTiltle=Title;
e_Address = Address;
}

Now how can I implement Constructor chaining in the above example.Sorry its a beginner level question. Any help will be greatly appreciated.

Thanking you in Anticipation.

cheers,
Sam



Answer this question

Constructor Chaining in C#

  • Roger England

    Well there will appear two constructors to the interface the main constructor with two arguments and the overloaded constructor with three arguments.

    But there will be one implementation of the constructor and the supplied code will call the one constructor with 2 arguments.

    Therefore the two variables e_name and e_JobTitle will contain the respective values.

    then you can write

    Console.WriteLine(e_name)
    Console.WriteLine(e_JobTitle)

  • Bezalel

    OK  keeping the following constructor in mind .

    public Class1(string name,string Title) : this(name, Title, "my default address")

    if I write

    Class1 myClass = new Class1("Lee","Developer");

    what will happen then. which constructor will be executed first and how can I see the values "Lee" and "Developer" on Console application. Any help would be greatly appreciated.

    cheers,
    Sam

  • dwhitson

    the 'chain constructors' ideas are that you write as little code as possible, and, cope with the fact that C# doesnt allow 'default arguments'. Also, the ctors must be 'public', or the world will not see them.

    Therefore, one writes

    public Class1(string name,string Title,string Address){   //   the "second" ctor 
       e_name =name;
       e_JobTiltle=Title;
       e_Address = Address;
    }

    and, the first ctor becomes

    public Class1(string name,string Title) : this(name, Title, "my default address")
    {}



  • oren

    you dont call constructors, they get called automatically when you create an instance of Class1.

    you would create an instance like this

    Class1 myClass = new Class1('Lee','Developer','1 London Road');

    Then the private variables e_name, e_JobTitle, e_Address would contain the values you passed in.

    As for accessing these values outside the class, you would need to create public propeties thet return these values.


  • DC Martin

    Dear Peter,

    Thanks for the prompt reply. So how do you call these constructors in the main programme. I mean how can I see the out put of e_name,e_JobTitle,e_Address

    cheers,
    Sam

  • ThiagoAlipio

    Adding on to Lee's comments, the 2-argument ctor "calls" the 3-arg ctor.

    You can also write
       Console.Writeline(e_Address);
    which will print
    "my default address"

    BTW, the "e_" prefix is unnecessary. "Preferred" would be field names like
    name, jobTitle, address, where the initial letter is lower case (so-called "camel casing").

  • zainshah

    You dont have to change anything in the above code.

    Basically constructor chaining is where a subclass calls its superclasses constructor which subsequentally calls its superclasses constrctor and so on.

    So if you were to create another class that inherits from the above base class then the derived class would be guaranteed to call the base classes constructor.

    So every derived class from there on would call its superclasses constructor until it gets the ther original base class.

    Thats constructor chaining.



  • Constructor Chaining in C#