By Sudipto Kumar Mukherjee • Microsoft Certified Trainer • .NET, C#, ASP.NET Core
If you prefer a quick walkthrough, watch the YouTube video below:
Primary constructors (new in C# 12 / .NET 8) let you declare constructor parameters directly on the type and use them inside the type body. This reduces boilerplate and keeps the intent close to the type name.
public class Person
{
public string Name { get; }
public int Age { get; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
public class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
Usage:
var p = new Person("Alice", 25);
Console.WriteLine($"{p.Name} is {p.Age} years old.");
public record Product(string Sku, decimal Price)
{
public bool InStock { get; init; } = true;
}
public struct Point(int x, int y)
{
public int X { get; } = x;
public int Y { get; } = y;
}
age < 0
).get;
-only or init;
properties for clear, immutable types.LangVersion
if your project is older.this.
or rename.Q: Are primary constructor parameters available outside the type?
A: No. Expose what you need through properties or fields.
Q: Can I add more constructors?
A: Yes. You can still define additional constructors or factory methods if required.
Q: Do they replace records?
A: No. Records are still great for value-like semantics; primary constructors simply reduce constructor boilerplate across types.
Primary constructors provide a concise, readable way to define simple types. Try them in your next C# 12 project to cut noise and focus on intent.
Contact: Sudipto Kumar Mukherjee – Microsoft Certified Trainer with 20+ years of experience in .NET, C#, ASP.NET Core.
📞 +91-9331897923 • 🌐 supernovaservices.com
🎬 Watch the video: Primary Constructors in C# 12
I prefer to start with a short 10-minute free call so I can understand:
Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.