C# 12 Primary Constructors – Cleaner Classes in One Line

C# 12 Primary Constructors – Cleaner Classes in One Line

By Sudipto Kumar Mukherjee • Microsoft Certified Trainer • .NET, C#, ASP.NET Core

Watch the Video

If you prefer a quick walkthrough, watch the YouTube video below:

Primary Constructors in C# 12 (YouTube)

What Are Primary Constructors?

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.

Before vs After

Traditional Constructor

public class Person
{
    public string Name { get; }
    public int Age { get; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

Primary Constructor (C# 12)

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.");

Works with Classes, Records, and Structs

Record

public record Product(string Sku, decimal Price)
{
    public bool InStock { get; init; } = true;
}

Struct

public struct Point(int x, int y)
{
    public int X { get; } = x;
    public int Y { get; } = y;
}

Best Practices & Tips

  • Keep it simple: Great for data-carrying types where constructor logic is minimal.
  • Expose properties you need: Parameters are not auto-properties; define properties/fields as usual.
  • Validation: You can still add guards inside the body (e.g., throw if age < 0).
  • Immutability: Combine with get;-only or init; properties for clear, immutable types.

Common Pitfalls

  • Version requirement: Needs C# 12 (typically .NET 8 SDK or later) and a matching LangVersion if your project is older.
  • Naming collisions: If you also declare a member with the same name as a parameter, qualify with this. or rename.
  • Large types: If a type grows complex, a traditional constructor may be clearer.

FAQ

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.

Conclusion

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.


Need Help?

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

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

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.



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50