Hey #dotnet community! 👋
C# 12 isn’t just another incremental update—it introduces practical features that streamline how we write and maintain code. As someone who’s been working with C# for years, I’m genuinely excited about how these changes reduce verbosity and boost readability.
My Top 3 Favourite Features
1. Primary Constructors
Finally, a cleaner way to handle constructor boilerplate in classes and structs!
For years, we've written the same repetitive constructor patterns:
public class User
{
private string _name;
public User(string name) { _name = name; }
}
C# 12 eliminates this ceremony with primary constructors:
public class User(string name) // Field auto-created!
{
public string Greet() => $"Hello, {name}!";
}
Why this matters:
Reduces up to 40% of boilerplate in DTOs/entities.
Works with structs **and **record classes too.
2. Collection Expressions
Unified syntax for arrays, lists, and spans.
No more switching between new[], List, and ImmutableArray syntaxes. Now:
int[] array = [1, 2, 3];
List list = [1, 2, 3];
Span span = [1, 2, 3];
Bonus: Works with Span for performance-critical scenarios.
Supports spread operator (..) for merging collections:
int[] combined = [..array1, ..array2]; // Like JavaScript, but type-safe!
3. Default Lambda Parameters
Lambdas now support optional parameters (e.g., (int x = 42) => x * 2), making them as flexible as methods.
var multiply = (int x, int y = 2) => x * y;
multiply(5); // Returns 10
Perfect for:
Configurable event handlers
Math utilities without helper methods
But Wait, There’s More!
*Interceptors *(experimental): A sneak peek into compile-time metaprogramming.
Alias Any Type: using aliases for tuples, arrays, and more.
Should You Upgrade?
Pros:
✅ Immediate productivity boost in new projects.
✅ Minimal breaking changes (unlike C# 11's required modifier).
Cons:
⚠️ Requires .NET 8+ or latest VS 2022.
I wrote a deep dive with code examples and comparisons to C# 11:
🔗 C# 12 Features: The Ultimate Guide
What’s your take?
Which feature will you use first?
Have you hit any **snags **adopting C# 12?
Let’s discuss below! 👇