C# – Call a constructor from another constructor

To call one constructor from another one, you have to use the constructor chaining syntax, like this:

public Person(string name) : this(name, birthDate: "N/A")
Code language: C# (cs)

This means when you use the Person(string name) constructor, it’ll first call the Person(string name, string birthDate) constructor.

If the constructor is in a base class, use base() instead of this():

public Employee(string name) : base(name) 
Code language: C# (cs)

Employee subclasses Person. So calling base(name) here means when you use the Employee(string name) constructor, it’ll first call the Person(string name) constructor.

Constructor call sequence

When you call a chained constructor, it executes the other constructor first (this() / base()). This is important to know so you don’t accidently overwrite values that were already set in the other constructor.

To show this call sequence, consider the following constructors:

public Person(string name, string birthDate)
{
	Name = name;
	BirthDate = birthDate;
}
public Person(string name) : this(name, "N/A")
{
	Console.WriteLine($"Properties: {Name} {BirthDate}");
}
Code language: C# (cs)

Now call the chained constructor:

 var person = new Person("Bob");
Code language: C# (cs)

This outputs the following, showing that it called the other constructor first (otherwise these properties would’ve been null here):

Properties: Bob N/ACode language: plaintext (plaintext)

Use default parameters instead of multiple constructors

When you want some construction parameters to be optional, your first instinct might be to add multiple constructors (and therefore need constructor chaining to de-duplicate). Consider using a single constructor with default parameters instead.

Here’s an example. Let’s say you have the following Movie class with two constructors and the rating parameter is optional:

public class Movie
{
	public readonly string Title;
	public readonly decimal? Rating;
	public Movie(string title, decimal rating) 
	{
		Title = title;
		Rating = rating;
	}
	public Movie(string title)
	{
		Title = title;
	}
}
Code language: C# (cs)

This can be simplified by using a single constructor with a default parameter:

public class Movie
{
	public readonly string Title;
	public readonly decimal? Rating;
	public Movie(string title, decimal? rating=null) 
	{
		Title = title;
		Rating = rating;
	}
}
Code language: C# (cs)

Use static methods to operate on parameters

In most cases, you can just pass constructor parameters as-is to the other constructor. But in some cases, you may need to do something to it before passing it in. The best way to do that is to use a static method (note: You can’t use an instance method).

For example, consider the following two constructors that accept a date as a string or DateTime:

public Person(string name, string birthDate)
{
	Name = name;
	BirthDate = DateTime.Parse(birthDate);
}
public Person(string name, DateTime birthDate)
{
	Name = name;
	BirthDate = birthDate;
}
Code language: C# (cs)

Let’s say you want to remove duplication by using constructor chaining:

public Person(string name, string birthDate) : this(name, DateTime.Parse(birthDate))
{
}
public Person(string name, DateTime birthDate)
{
	Name = name;
	BirthDate = birthDate;
}
Code language: C# (cs)

Notice that you’re able to use the static method DateTime.Parse() to operate on the birthDate parameter.

If there isn’t a built-in static method for whatever you’re doing, add your own to the class. For example, let’s say you want to try to parse the date string and pass in null as the default if can’t be parsed. You can put this in a static method:

public Person(string name, string birthDate)
	: this(name, GetDateOrDefault(birthDate))
{
}
private static DateTime? GetDateOrDefault(string dateTime)
{
	if (DateTime.TryParse(dateTime, out DateTime parsedDateTime))
		return parsedDateTime;

	return null;
}

public Person(string name, DateTime? birthDate)
{
	Name = name;
	BirthDate = birthDate;
}
Code language: C# (cs)

Alternative – Extract common code to a helper method

If you don’t want to use constructor chaining, you can extract common code to a helper method instead and call it from all of the constructors. Here’s an example:

public Movie(string title, decimal rating)
{
	Rating = rating;
	Init(title);
}
public Movie(string title)
{
	Init(title);
}
private void Init(string title)
{
	Title = title;
}
Code language: C# (cs)

This is a simple alternative to using constructor chaining.

Readonly and init-only properties are a problem

You can’t directly set readonly and init-only properties in the helper method.

The simple solution is don’t set it in the helper method. Leave it in the constructor(s). If the logic for calculating the property’s value is complex, extract a method to get the calculated value call it from the constructor(s).

There’s a more complicated solution, which I wouldn’t recommend unless you’re forced into it for some reason. You can pass the readonly property by ref to be able to set it in the helper method:

public readonly string Title;
public Movie(string title)
{
	Init(ref Title, title);
}
private void Init(ref string TitleRef, string title)
{
	TitleRef = title;
}
Code language: C# (cs)

This doesn’t work with init-only properties.