Primary constructors, introduced in C# 12, offer a more concise way to define class parameters and initialize fields.
This feature reduces boilerplate code and makes classes more readable.
Traditional Approach vs Primary Constructor
Before primary constructors, you would likely write something like the following:
public class UserService
{
private readonly ILogger _logger;
private readonly IUserRepository _repository;
public UserService(ILogger logger, IUserRepository repository)
{
_logger = logger;
_repository = repository;
}
public async Task<User> GetUserById(int id)
{
_logger.LogInformation("Fetching user {Id}", id);
return await _repository.GetByIdAsync(id);
}
}
With primary constructors, this becomes:
public class UserService(ILogger logger, IUserRepository repository)
{
public async Task<User> GetUserById(int id)
{
logger.LogInformation("Fetching user {Id}", id);
return await repository.GetByIdAsync(id);
}
}
Key Benefits
- Reduced Boilerplate: No need to declare private fields and write constructor assignments
- Parameters Available Throughout: Constructor parameters are accessible in all instance methods
- Immutability by Default: Parameters are effectively readonly without explicit declaration
Real-World Example
Here's a practical example using primary constructors with dependency injection:
public class OrderProcessor(
IOrderRepository orderRepo,
IPaymentService paymentService,
ILogger<OrderProcessor> logger)
{
public async Task<OrderResult> ProcessOrder(Order order)
{
try
{
logger.LogInformation("Processing order {OrderId}", order.Id);
var paymentResult = await paymentService.ProcessPayment(order.Payment);
if (!paymentResult.Success)
{
return new OrderResult(false, "Payment failed");
}
await orderRepo.SaveOrder(order);
return new OrderResult(true, "Order processed successfully");
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to process order {OrderId}", order.Id);
throw;
}
}
}
Tips and Best Practices
- Use primary constructors when the class primarily needs dependencies for its methods
- Combine with records for immutable data types:
public record Customer(string Name, string Email)
{
public string FormattedEmail => $"{Name} <{Email}>";
}
- Consider traditional constructors for complex initialization logic
Primary constructors provide a cleaner, more maintainable way to write C# classes, especially when working with dependency injection and simple data objects.