Skip to main content
Formatted code to make it clearer and to follow best practices
Source Link
Pat
  • 2.9k
  • 2
  • 25
  • 35

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(456,"def"), c = new Foo(123), d = new Foo("abc"); 

Note 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 }; 

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(456,"def"), c = new Foo(123), d = new Foo("abc"); 

Note 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 }; 

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(456,"def"), c = new Foo(123), d = new Foo("abc"); 

Note 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 }; 
Made the implementation in the correct order, implementation b was not the second constructor
Source Link

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(123456,"def"), c = new Foo("abc"123), d = new Foo(456,"def""abc"); 

Note 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 }; 

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"); 

Note 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 }; 

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(456,"def"), c = new Foo(123), d = new Foo("abc"); 

Note 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 }; 
Fix simple typo.
Source Link
Scott
  • 17.3k
  • 5
  • 56
  • 65

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"); 

NodeNote 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 }; 

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 }; 

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"); 

Note 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 }; 
Source Link
Marc Gravell
  • 1.1m
  • 273
  • 2.6k
  • 3k
Loading