C# 12 (released with .NET 8) is all about reducing "Boilerplate." It allows you to express complex ideas with significantly less code, making your codebase easier to maintain.
Historically, you had to define a constructor and manually assign fields. Now, you can define parameters directly on the class header:
public class UserService(IDbContext db, ILogger logger) {
public void Save() => logger.Log("Saving to " + db.Name);
}
**Architect Tip:** Primary constructors automatically create "Private Fields" for you, but be careful—they are mutable! Use them primarily for Dependency Injection in services.
No more `new List
The compiler is smart enough to optimize the memory allocation based on the target type. It even supports the **Spread Operator** (List..) to merge collections.
Q: "Why should you use 'Default Lambda Parameters' in C# 12?"
Architect Answer: "Before C# 12, lambdas didn't support default values. Now they do: `(int x = 1) => x + 1`. This makes it much easier to write 'Minimal APIs' and 'Route Handlers' in ASP.NET Core without needing overloads for every possible parameter combination. it keeps your API definition clean and concise."