Want immutable objects but constructors with 10 parameters are ugly? Use init accessors.
Old Way – Constructor Overload Hell:
public class Person
{
public string Name { get; }
public int Age { get; }
public string Email { get; }
public Person(string name, int age, string email)
{
Name = name;
Age = age;
Email = email;
}
}
var person = new Person("John", 30, "john@example.com");
New Way – init Accessors:
public class Person
{
public string Name { get; init; }
public int Age { get; init; }
public string Email { get; init; }
}
var person = new Person
{
Name = "John",
Age = 30,
Email = "john@example.com"
};
person.Name = "Jane"; // ❌ Compiler error - can't change after init
Benefits: Object initialization syntax, but properties are immutable after construction. No constructor needed!
