views:

462

answers:

3

Hi all, I know that this is supposedly a super simple question, but I've been struggling with the concept for some time now. My question is, how do you chain constructors in c#? I'm in my first OOP class, so I'm just learning. I don't understand how constructor chaining works or how to implement it, or even why it's better than just doing constructors without chaining.

I would appreciate some examples with an explanation.

So how do how chain them? I know with two it goes:

public SomeClass this: {0}

public SomeClass
{
    someVariable = 0
}

But how do you do it with three, four and so on?

Again, I know this is a beginner question, but I'm struggling to understand this and I don't know why.

Thanks a bunch!

+9  A: 

You use standard syntax (using this like a method) to pick the overload, inside the class:

class Foo {
    private int id;
    private string name;
    public Foo() : this(0, "") {
    }
    public Foo(int id, string name) {
        this.id = id;
        this.name = name;
    }
    public Foo(int id) : this(id, "") {
    }
    public Foo(string name) : this(0, name) {
    }
}

then:

Foo a = new Foo(), b = new Foo(123), c = new Foo("abc"), d = new Foo(456,"def");

Node also:

  • you can chain to constructors on the base-type using base(...)
  • you can put extra code into each constructor
  • the default (if you don't specify anything) is base()

For "why?":

  • code reduction (always a good thing)
  • necessary to call a non-default base-constructor, for example:

    SomeBaseType(int id) : base(id) {...}
    

Note that you can also use object initializers in a similar way, though (without needing to write anything):

SomeType x = new SomeType(), y = new SomeType { Key = "abc" },
         z = new SomeType { DoB = DateTime.Today };
Marc Gravell
+2  A: 

Are you asking about this?

  public class VariantDate {
    public int day;
    public int month;
    public int year;

    public VariantDate(int day) : this(day, 1) {}

    public VariantDate(int day, int month) : this(day, month,1900){}

    public VariantDate(int day, int month, int year){
    this.day=day;
    this.month=month;
    this.year=year;
    }

}
Amby
+4  A: 

This is best illustrated with an example. Imaging we have a class Person

public Person(string name) : this(name, string.Empty)
{
}

public Person(string name, string address) : this(name, address, string.Empty)
{
}

public Person(string name, string address, string postcode)
{
    this.Name = name;
    this.Address = address;
    this.Postcode = postcode;
}

So here we have a constructor which sets some properties, and uses constructor chaining to allow you to create the object with just a name, or just a name and address. If you create an instance with just a name this will send a default value, string.Empty through to the name and address, which then sends a default value for Postcode through to the final constructor.

In doing so you're reducing the amount of code you've written. Only one constructor actually has code in it, you're not repeating yourself, so, for example, if you change Name from a property to an internal field you need only change one constructor - if you'd set that property in all three constructors that would be three places to change it.

blowdart