Articles → CSHARP → Constructor Chaining In C#
Constructor Chaining In C#
Purpose
Example
public className() : this(parameter1, parameter2,....)
using System;
namespace ConstructorChaining {
class Program {
static void Main(string[] args) {
ConstructorChainingDemo constructorChainingDemo = new ConstructorChainingDemo();
Console.ReadLine();
}
}
public class ConstructorChainingDemo {
public ConstructorChainingDemo() : this("test") {
Console.WriteLine("Calling default constructor");
}
public ConstructorChainingDemo(string value) {
Console.WriteLine("Calling parameterized constructor with value:" + value);
}
}
}
Output
Click to Enlarge