🔒 Immutable + Flexible
Readonly properties need constructor parameters. Init accessors let you use object initializers.
// C# 9+
public class User
{
public int Id { get; init; }
public string Name { get; init; }
public string Email { get; init; }
}
// Can set during initialization
var user = new User
{
Id = 1,
Name = "John",
Email = "john@example.com"
};
// Cannot modify after
user.Name = "Jane"; // ❌ Compile error!
Benefit: Immutability without constructor bloat. Clean object creation syntax.
With Record: public record User(int Id, string Name); – even shorter!
