Primary Constructors in C#


  • Primary Constructors are a new feature in C# 12. The initialization of properties in classes is made simpler by this feature, especially for classes with many of properties. The function of Primary Constructors will be examined in this article.
  • Let’s examine class constructors in C# first before moving on to primary constructors. When a class object is formed in C#, a constructor is a unique method that is called. Initializing an object’s state, which normally entails setting the values of the class properties, is done using constructors.

Here’s an example of a typical class with a constructor in C#,

public class Employee
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime HireDate { get; set; }
    public decimal Salary { get; set; }

    public Employee(string firstName, string lastName, DateTime hireDate, decimal salary)
        FirstName = firstName;
        LastName = lastName;
        HireDate = hireDate;
        Salary = salary;

What are Primary Constructors?

  • With the introduction of the Primary Constructor in C# 12, it is now possible to create and initialize properties right in the constructor parameter list. Your code will be shorter and easier to understand thanks to this feature, which also reduces the need for repetitive code.

Using Primary Constructors

Let’s start with a primary constructor example utilizing the Employee class. Here is how you would create a Primary Constructor to define an Employee class:

public class Employee(string firstName, string lastName, DateTime hireDate, decimal salary)
    public string FirstName { get; init; } = firstName;
    public string LastName { get; init; } = lastName;
    public DateTime HireDate { get; init; } = hireDate;
    public decimal Salary { get; init; } = salary;
  • The Employee class in this example has the following four properties: FirstName, Last Name, Hire Date, and Salary. Additionally, we made the properties read-only by using the init keyword, and we initialized them right inside the Primary Constructor’s argument list.

Here’s how you would create a new Employee object using the Primary Constructor.

var employee = new Employee("John", "Doe", new DateTime(2020, 1, 1), 50000);
  • The first Name, last name, hire Date, and salary arguments are used to initialize the respective properties when the Employee object is formed.
  • You’ll get a compiler error if you attempt to build an object using the default constructor since the Employee class doesn’t have a constructor function with a parameter less constructor.
  • Classes with a lot of attributes benefit from primary constructors since they require less boilerplate code to initialize them. Using primary constructors also improves the readability of your code and lessens the possibility of problems brought on by missing or erroneously placed initialization statements.

Conclusion :-

Primary Constructors, a new feature in C# 12, make it easier to define and initialize class attributes.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories