How to Use a Primary Constructor in a C# Class
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
class <ClassName>(<Type> <parameter>, <Type> <parameter>)
{
<members that use the parameters>
}
| Part | What 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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
dotnet run main.cs
Hello, Leo!
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:
var greeter = new Greeter("Leo");
Console.WriteLine(greeter.name);
class Greeter(string name)
{
public string Greet() => $"Hello, {name}!";
}
/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:
var greeter = new Greeter("Leo");
Console.WriteLine(greeter.Name);
class Greeter(string name)
{
public string Name { get; } = name;
}
Leo
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading the parameter from outside, as in greeter.name | error CS1061: 'Greeter' does not contain a definition for 'name' | Add a property: public string Name { get; } = name; |
Writing this.name inside the class | It isn't a member, so this.name doesn't exist | Write name |
| Expecting the class to get properties like a record | Only records turn their parameters into properties | Declare the properties you need, or use a record |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS1061 for a primary constructor parameter | It was read from outside the class | Expose it as a property |
A build error at class Greeter(...) | The language version is older than C# 12 | Use 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
- Primary constructors, on Microsoft Learn.
- Instance constructors: primary constructors, on Microsoft Learn.
