Skip to main content

How to Use a Primary Constructor in a C# Class

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A classic C# constructor copies each parameter into a field before methods can use it. A primary constructor skips that: write the parameters in parentheses after the class name, class Greeter(string name), and every method in the class can use name directly.

Syntax​

Syntax
class <ClassName>(<Type> <parameter>, <Type> <parameter>)
{
<members that use the parameters>
}
PartWhat it does
class <ClassName>(...)Declares the class and its constructor parameters in one line.
<Type> <parameter>A constructor parameter, available in the whole class body.
new <ClassName>(<arguments>)Creates an object, passing a value for each parameter.

Primary constructors arrived in C# 12. Microsoft's primary constructors tutorial: "Primary constructors provide a concise syntax to declare constructors whose parameters are available anywhere in the body of the type." The constructors guide: "The parameters to a primary constructor are in scope in the entire body of the declaring type."

The Example​

A Greeter class takes a name in its primary constructor. Its Greet method uses the name, and its Shout method calls Greet and turns the result into capitals.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which creates a Greeter for Leo and prints Greet() and Shout(), where class Greeter(string name) has a Greet method returning Hello with the name and a Shout method returning Greet() in upper case.

main.cs
var greeter = new Greeter("Leo");
Console.WriteLine(greeter.Greet());
Console.WriteLine(greeter.Shout());

class Greeter(string name)
{
public string Greet() => $"Hello, {name}!";

public string Shout() => Greet().ToUpper();
}

There's no field and no constructor body: Greet reads name straight from the primary constructor.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Hello, Leo!
HELLO, LEO!

A zsh terminal where dotnet run main.cs prints Hello, Leo! and HELLO, LEO!

new Greeter("Leo") passed Leo to the primary constructor, and both methods used it. The command is the same in zsh, bash and PowerShell.

The Parameter Isn't a Property​

Code outside the class can't read name:

main.cs
var greeter = new Greeter("Leo");
Console.WriteLine(greeter.name);

class Greeter(string name)
{
public string Greet() => $"Hello, {name}!";
}
Output
/Users/you/demo/main.cs(2,27): error CS1061: 'Greeter' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'Greeter' could be found (are you missing a using directive or an assembly reference?)

The build failed. Fix the build errors and run again.

The tutorial explains: "Primary constructor parameters aren't members of the class. For example, a primary constructor parameter named param can't be accessed as this.param." And: "Primary constructor parameters don't become properties, except in record types."

To make the value public, declare a property and set it from the parameter:

main.cs
var greeter = new Greeter("Leo");
Console.WriteLine(greeter.Name);

class Greeter(string name)
{
public string Name { get; } = name;
}
Output
Leo

Common Mistakes​

MistakeWhat happensDo this instead
Reading the parameter from outside, as in greeter.nameerror CS1061: 'Greeter' does not contain a definition for 'name'Add a property: public string Name { get; } = name;
Writing this.name inside the classIt isn't a member, so this.name doesn't existWrite name
Expecting the class to get properties like a recordOnly records turn their parameters into propertiesDeclare the properties you need, or use a record

Troubleshooting​

SymptomLikely causeFix
CS1061 for a primary constructor parameterIt was read from outside the classExpose it as a property
A build error at class Greeter(...)The language version is older than C# 12Use the .NET 8 SDK or newer, which defaults to C# 12 or later

FAQ​

What's the difference from a record?​

A positional record, such as record Person(string Name), turns each parameter into a public property. A class with a primary constructor doesn't: its parameters stay private to the class body unless you declare properties for them.

Can I still add other constructors?​

Yes, but each one has to call the primary constructor. The constructors guide: "Any explicitly written constructor must use the this(...) initializer syntax to invoke the primary constructor." That way the parameters always get a value.

Conclusion​

class Greeter(string name) declares the constructor parameters next to the class name, and every member of the class can use name without a field. The parameter stays inside the class: expose it with a property when code outside needs it.

Additional Resources​