Primary Constructor in C#

Primary constructors are a new feature in C# 12 that allows you to declare and use constructor parameters anywhere in the body of a class or a struct. They are a concise way to initialize properties or fields, call base constructors, or reference the parameters in other members of the type.

For example, you can declare a class with a primary constructor like this:

public class Person(string name, int age)
{
    // primary constructor parameters are in scope here
}

The primary constructor parameters name and age are not public properties, but they can be used to initialize public properties, like this:

public class Person(string name, int age)
{
    public string Name { get; } = name; // initialize a readonly property
    public int Age { get; set; } = age; // initialize a read-write property
}

You can also use primary constructor parameters to call a base constructor, like this:

public class Student(string name, int age, string school) : Person(name, age)
{
    public string School { get; } = school;
}

The primary constructor of Student calls the primary constructor of Person with the name and age parameters and then initializes its own property School with the school parameter.

You can also reference primary constructor parameters in other members of the type, such as methods, fields, or expression-bodied members, like this:

public class Circle(double radius)
{
    public double Area => Math.PI * radius * radius; // expression-bodied property
    public double Circumference => 2 * Math.PI * radius; // expression-bodied property

    public void PrintInfo()
    {
        Console.WriteLine($"A circle with radius {radius} has area {Area} and circumference {Circumference}");
    }
}

The primary constructor parameter radius is used to calculate the circle's area and circumference and print the information in the PrintInfo method.

Primary constructors are useful when you want to reduce the amount of boilerplate code you need to write and when you want to have a consistent way to access constructor parameters throughout the type. However, there are some limitations and rules you need to be aware of when using primary constructors:

  • Primary constructor parameters are not stored as fields unless they are needed. This means they do not consume memory or affect the size of the type.
  • Primary constructor parameters are not members of the type. This means you cannot access them using this or base or use them in attribute arguments or default parameter values.
  • Primary constructor parameters can be assigned to them, but they are not properties. This means they do not have getters or setters, and they do not support attributes or modifiers.
  • Primary constructor parameters are only available in the type where they are declared. This means derived types do not inherit them and are not visible to external callers.
  • Every other constructor for a class must call the primary constructor, directly or indirectly, through this() constructor invocation. This ensures that primary constructor parameters are always assigned and available in the type.

Learn more about New Features of .NET 8


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.