What Are Primary Constructors in C# 12?


The C# programming language has advanced significantly with version 12, which is distinguished by its emphasis on improving code clarity and development efficiency. Numerous new features are included in this version to improve functionality and streamline the code structure. For example, adding primary constructors simplifies class and struct definitions, resulting in a codebase that is easier to read and comprehend. C#'s dedication to efficiency and usability is further demonstrated by other features such as ref readonly arguments, inline arrays, and better collection expressions. C# 12 represents a significant update in the C# evolution timeline since it includes these new capabilities, which demonstrate a continued effort to expand the language in response to developer requests.

One noteworthy addition to C# 12 that improves both the language's functionality and developer experience is the use of primary constructors. They lessen boilerplate code and simplify syntax by enabling developers to declare constructor parameters directly in the class or struct declaration. This feature allows for more understandable and concise object initialization, which is especially useful for classes with lots of properties. Primary constructors make C# even more flexible and effective for contemporary programming techniques by incorporating constructor parameters into the declaration, which speeds up object generation and encourages a cleaner, more ordered code structure.

Primary Constructors in C#

With C# 12, primary constructors enable constructor parameters to be declared right in the class or struct declaration line. Conventional constructors require you to declare a constructor method directly, set properties inside of it, and frequently write extra lines of code for every property. This can lead to repetitious and verbose code, particularly for classes with many properties.

With C# 12, constructor parameters can be declared directly in the class or struct definition line when using primary constructors. By using the supplied arguments to automatically initialize properties, this method minimizes boilerplate code and improves readability. Because the property declaration and initialization are consolidated into a single line and the class structure is made simpler, the code is more concise.

Compared to conventional constructors, this new method eliminates the need for extra code lines by streamlining the initialization of object properties. Primary constructors provide a cleaner, more effective method of handling object initialization by putting the constructor parameters directly into the class or struct declaration, improving the readability and maintainability of the code.

Classes that are primarily used as data containers will especially benefit from this, as it lowers the overall complexity and verbosity of the code.

Let's look at an example to illustrate primary constructors in C# 12.

In traditional C# versions, if you wanted to create a class with several properties and initialize them through a constructor, you'd typically write something like this.

public class Person
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
        Name = name;
        Age = age;

With primary constructors in C# 12, you can simplify this pattern. The same class can be written.

public class Person(string Name, int Age);

In this new syntax, Name, and Age are constructor parameters and are automatically available as properties of the Person class. This approach makes the code more concise and readable, especially for classes with a large number of properties.

By enabling the direct inclusion of constructor parameters in the class or struct header, primary constructors in C# 12 significantly reduce the complexity of class and struct declarations. As a result, fewer independent property declarations and constructor bodies are required. This results in reduced code, simpler upkeep, and enhanced readability—particularly for types that are primarily used as data containers or straightforward models. It's a condensed method of defining structs and classes, which improves code readability and efficiency.

Syntax and Usage

In C# 12, primary constructors have a simple and clear syntax. This format does not require a separate constructor method or explicit property assignments because the constructor parameters are declared directly in the class or struct definition line. Constructor definitions and property initializations can be done in a single line, which streamlines the class or struct's overall structure and improves readability and maintainability. This method can greatly minimize the amount of boilerplate code in classes or structs that contain a lot of data.

Here are two examples to illustrate the use of primary constructors in C# 12, one for a class and one for a struct.

Class Example with Primary Constructor.

public class Employee(string Name, int Age, string Department);

In this example, the Employee is a class with three properties: Name, Age, and Department. The primary constructor syntax allows these properties to be defined and initialized directly within the class declaration.

Struct Example with Primary Constructor

public struct Point(int X, int Y);

Here, Point is a struct with two properties: X and Y. The primary constructor simplifies the struct declaration by allowing direct initialization of these properties.

The parameters declared in the class or struct header in C# 12's main constructors have a scope that spans the whole body of the class or struct. This means that these arguments are not only available for the initial property setting but can also be utilized elsewhere within the class or struct, just like standard class-level properties or fields. More flexibility in applying these parameters is made possible by their wider scope, which enables more succinct and adaptable class designs.

Practical Applications

In situations where a class or struct is primarily utilized as a data container, C# 12's primary constructors come in handy. Among these scenarios.

  • Data Models: Classes are mostly used to represent data structures with little to no business logic in applications that operate with data transfer objects (DTOs) or models in frameworks like ASP.NET.
  • Immutable Objects: Used to create immutable classes or structs, in which properties are initialized and remain constant during object construction. The simple construction of such immutable types is made easier by primary constructors.
  • Simplifying Record Types: Primary constructors can help to further simplify the definitions of record types, which in C# already offer a clear means of defining data-centric types.
  • Eliminating Boilerplate Code: In any situation where the objective is to lessen the verbosity of the code, particularly in structs or classes that have a lot of properties that require initialization.

Here are a few real-world application code snippets using primary constructors in C# 12.

Web API Data Model

public class Product(int Id, string Name, decimal Price, string Category);

In a web API, this Product class could represent a data model for products in an e-commerce application. The primary constructor simplifies the declaration by directly initializing properties needed for the product details.

Configuration Settings

public class AppSettings(string ConnectionString, bool EnableCaching, int CacheDuration);

This AppSettings class might be used to hold configuration settings in an application. The primary constructor allows for a straightforward and clean initialization of various configuration properties.

Immutable Objects Example

public ImmutablePoint(int X, int Y)
    public void Deconstruct(out int x, out int y)
        x = X;
        y = Y;

This ImmutablePoint class represents a point in a 2D space. The properties X and Y are set only once during object creation and cannot be modified thereafter, making the object immutable.

Simplifying Record Types Example

public record Customer(string Name, string Email, int Age);

This Customer record is a simplified representation of a customer data type. Records in C# are by default immutable and well-suited for data modeling. Primary constructors further streamline their declaration.

These examples demonstrate the effectiveness of primary constructors in scenarios where classes are primarily used for data representation, and how primary constructors can be used to simplify record types, create immutable objects, improve code clarity, and cut down on boilerplate code.

In C# 12, primary constructors—as opposed to traditional constructor syntax—significantly improve readability and code simplicity. Much less code is needed to accomplish the same functionality when using primary constructors. The properties are created and initialized automatically by the constructor parameters, which are declared directly in the class header. This shortens the code's lines and improves readability and glance comprehension of the class specification. Because there is less boilerplate code, the code is easier to maintain because updates to the properties only need to be made once.

Limitations and Considerations

There are a few restrictions and things to think about while utilizing primary constructors in C# 12.

  • Compatibility with Existing Code: Adding primary constructors to an existing codebase may cause issues, particularly if complex initialization patterns or custom constructor logic are heavily relied upon.
  • Limitation in Customization: Primary constructors are less flexible than traditional constructors when it comes to intricate initialization logic and conditional property settings.
  • Interoperability Concerns: Primary constructors may cause compatibility problems when interacting with previous versions of C# or other languages.
  • Debugging and Overloading: Since the constructor logic is implicit, debugging may be more difficult. Furthermore, overloading constructors may call for further attention.

When considering whether to utilize primary constructors in a particular situation, it's critical to balance these considerations against the advantages of clearer, more concise code.

Primary constructors in C# 12 have certain interactions and considerations when it comes to inheritance:

  • Constructor Inheritance: In the event that a base class employs a primary constructor, derived classes must handle the constructor parameters of the base class correctly, either by calling the base constructor directly or by passing them through their own primary constructor.
  • Overriding Behavior: Unlike methods, primary constructors cannot be overwritten. For more complicated situations, they can be supplemented with more constructors in the derived class.
  • Constructor Chaining: In a class hierarchy, primary constructors can engage in constructor chaining, much like traditional constructors, to guarantee correct initialization all the way down the inheritance chain.

These interactions show that when utilizing primary constructors, class hierarchies and constructors must be carefully planned for the best design and functionality.


In C# 12, primary constructors can have a big influence on how people code. They make code more condensed and legible by streamlining class and struct declarations, especially for objects with lots of data. This feature makes handling object initialization easier and makes it easier to create immutable types. It does, however, need to be carefully considered in codebases with sophisticated initialization logic and in complex inheritance circumstances. In general, primary constructors encourage simpler, more manageable code structures by modernizing and streamlining C# coding.

It is important for readers to experiment with primary constructors in their C# projects in order to consolidate this knowledge. This capability opens up new opportunities for cleaner and more efficient programming. Those who work with data models or need to develop immutable objects may find it especially helpful. Developers can experiment with new design patterns and enhance the readability and maintainability of their projects by incorporating primary constructors into their code. As with any new feature, practical exploration in real-world settings is the only approach to fully grasp its advantages and limits.


Similar Articles