C# Constructors

Summary: in this tutorial, you’ll learn about the C# constructors and how to use them to initialize objects.

Introduction to the C# constructors

A constructor is a special method whose name is the same as the class name. The constructor is special because of the following reasons:

  • A constructor doesn’t have the return type.
  • When creating a new object, the C# compiler automatically calls the constructor.

Typically, you use a constructor to initialize the new object by assigning values to the object’s properties.

The C# compiler implicitly creates a constructor without any parameter when defining a class. A constructor without a parameter is called a parameterless constructor.

For example, the following defines a Person class:

// Person.cs

class Person
{
}Code language: C# (cs)

C# implicitly creates a constructor without any parameters like this:

// Person.cs

class Person
{
    public Person()
    {
    }
}Code language: C# (cs)

The following example illustrates how C# implicitly calls the constructor when you create a new object:

// Person.cs

class Person
{
    public Person()
    {
        Console.WriteLine("Create a new Person object");
    }
}Code language: C# (cs)
// Program.cs

Person p1 = new();Code language: C# (cs)

Output:

Create a new Person objectCode language: plaintext (plaintext)

Define a constructor

The following example defines a constructor with three parameters for the Person class:

// Person.cs

class Person
{
    private string firstName;
    private string lastName;
    private byte age;

    public Person(string firstName, string lastName, byte age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public string GetFullName()
    {
        return $"{this.firstName} {this.lastName}";
    }
}Code language: C# (cs)

In this example, the constructor accepts three arguments and initializes the private properties firstName, lastName, and age.

Once you define an explicit constructor, the C# compiler doesn’t implicitly add the parameterless constructor to the class.

Therefore, to create a new Person‘s object, you need to pass three arguments like the following:

// Program.cs

var p1 = new Person("John", "Doe", 25);Code language: C# (cs)

Or

Person p2 = new("John", "Doe", 25);Code language: C# (cs)

Define multiple constructors for a class

C# allows you to define multiple constructors with different parameters for a class. For example:

// Person.cs

class Person
{
    private string firstName;
    private string lastName;
    private byte age;

    public Person()
    {
    }

    public Person(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Person(string firstName, string lastName, byte age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}Code language: C# (cs)

In this example, the Person class has three constructors.

  • The first constructor is a parameterless constructor that accepts no parameter.
  • The second constructor accepts two parameters and initializes the firstName and lastName properties.
  • The third constructor accepts three parameters and initializes the firstName, lastName, and age properties.

By doing this, you can create a new Person object by passing no parameter, two parameters, or three parameters to the respective constructors:

// Program.cs

var p1 = new Person();
var p2 = new Person("John","Doe");
var p3 = new Person("John", "Doe",25);Code language: C# (cs)

The code in the constructors is duplicated in initializing the firstName and lastName properties. To fix this, you can call a constructor in another constructor as shown in the following example:

// Person.cs

class Person
{
    private string firstName;
    private string lastName;
    private byte age;

    public Person()
    {
        Console.WriteLine("Initialized the object");
    }

    public Person(string firstName, string lastName) : this()
    {
        this.firstName = firstName;
        this.lastName = lastName;

        Console.WriteLine("Initialized the first name & last name properties");
    }

    public Person(string firstName, string lastName, byte age) : this(firstName, lastName)
    {
        this.age = age;
        Console.WriteLine("Initialized the age property");
    }
}Code language: C# (cs)

In this example, the second constructor calls the first constructor via the this keyword:

public Person(string firstName, string lastName) : this()Code language: C# (cs)

When you create an object with two parameters, this constructor will call the parameterless constructor (this()) before executing its body.

The third constructor calls the second constructor via the this keyword with the firstName and lastName arguments:

public Person(string firstName, string lastName, byte age) : this(firstName, lastName)Code language: C# (cs)

If you create a new object with three arguments, the third constructor will call the second constructor, which calls the first constructor.

Therefore, the constructors will execute in the following sequence:

public Person()
public Person(string firstName, string lastName) : this()
public Person(string firstName, string lastName, byte age) : this(firstName, lastName)Code language: C# (cs)

For example:

// Program.cs

var p1 = new Person("John","Doe", 25);Code language: C# (cs)

Output:

Initialized the object
Initialized the first name & last name properties
Initialized the age propertyCode language: plaintext (plaintext)

By doing this, you can reuse the logic of other constructors when initializing the objects

Summary

  • C# constructor is a special method whose name is the same as the class name.
  • C# compiler automatically calls the constructor when you create a new object.
  • Use the constructors to initialize objects.
  • Use multiple constructors in a class to make it more flexible to initialize a new object.
Was this tutorial helpful ?