Primary Constructure make your code clear .net 8

Tohid haghighi
3 min readMar 11, 2024

--

C# 12 introduces a new feature called Primary Constructors. This feature aims to simplify the initialization of properties in classes, especially for classes with many properties. This article will explore Primary Constructors and see how they work.

C# 12

Before diving into Primary Constructors, let’s look at how class constructors work in C#. In C#, a constructor is a special method called when a class object is created. Constructors are used to initialising an object’s state, which typically involves setting the values of the class properties.

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?

A Primary Constructor is a new feature in C# 12 that allows you to define and initialize properties directly within the constructor parameter list. This feature eliminates the need for repeated code and makes your code more concise and readable.

Using Primary Constructors

Let’s start with an example of using Primary Constructors with an Employee class. Here’s how you would define an Employee class using a Primary Constructor.

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;
}

In this example, we’ve defined an Employee class with four properties: FirstName, LastName, HireDate, and Salary. We’ve also used the init keyword to make the properties read-only and initialized them directly within the Primary Constructor’s parameter list.

In the other Example think you have a Service class that you need to inject some other Service or Api to use in implement functions, primary constructure can help you to make your code clear and readable.

Use dependency injection in primary constructors in C#

You can take advantage of primary constructors when implementing dependency injection in your C# application. Let us understand this with an example. The following code illustrates how we can implement constructor injection in C#.

public class AuthorService
{
private readonly IAuthorRepository _authorRepository;
public AuthorService(IAuthorRepository authorRepository)
{
_authorRepository = authorRepository;
}
public async Task<IEnumerable<Author>> GetAll()
{
return await _authorRepository.GetAll();
}
}

Finally, note how primary constructors can be used to make your code crisper and cleaner, as shown in the following piece of code.

public class AuthorService (IAuthorRepository authorRepository)
{
public async Task<IEnumerable<Author>> GetAll()
{
return await authorRepository.GetAll();
}
}

Although primary constructors were initially introduced in C# 9, they were limited to record types only. With C# 12, you can use primary constructors in record types, classes, and structures, helping you write boilerplate code that is cleaner and more concise.

--

--

Tohid haghighi
Tohid haghighi

Written by Tohid haghighi

Full-Stack Developer | C# | .NET Core | Vuejs | TDD | Javascript

No responses yet