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:

C#
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:

C#
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:

C#
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:

C#
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:

Learn more about New Features of .NET 8