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


Picture showing the output of Constructor chaining in C#
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, August 17, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250