What if you could set a property once and be sure it never changes by mistake?
Why Init-only setters in C Sharp (C#)? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to create a person record in your program and set their name and age. You write code that sets these values one by one after creating the person object.
Later, you realize you want to make sure these values never change once set, but your code allows changes anywhere, causing bugs.
Manually preventing changes after setting values means extra code everywhere to check if a value can be changed.
This is slow, error-prone, and makes your code messy and hard to maintain.
Init-only setters let you set properties only once during object creation.
After that, the properties become read-only automatically, so you don't need extra checks.
This keeps your code clean and safe from accidental changes.
var person = new Person(); person.Name = "Alice"; person.Age = 30; // Later someone changes person.Name = "Bob"; // no restriction
var person = new Person { Name = "Alice", Age = 30 };
// person.Name = "Bob"; // error: cannot change after initYou can create objects with properties that are set once and then stay fixed, making your programs more reliable and easier to understand.
When creating a configuration object for an app, you want to set values once at startup and never change them accidentally during runtime.
Init-only setters allow setting properties only during object creation.
This prevents accidental changes later, reducing bugs.
It makes your code cleaner and safer without extra checks.
Practice
init-only setters in C#?Solution
Step 1: Understand init-only setters
Init-only setters allow properties to be set only when the object is created, not after.Step 2: Compare options
Only To allow setting properties only during object creation correctly describes this behavior; others are unrelated or incorrect.Final Answer:
To allow setting properties only during object creation -> Option BQuick Check:
Init-only setters = set only at creation [OK]
- Thinking init-only allows changes anytime
- Confusing init-only with readonly fields
- Assuming init-only affects methods
Solution
Step 1: Recall init-only syntax
Init-only properties useinitinstead ofsetto allow setting only during initialization.Step 2: Check options
public string Name { get; init; } uses{ get; init; }, which is correct syntax. Others useset,readonly(invalid here), orprivate set.Final Answer:
public string Name { get; init; } -> Option AQuick Check:
Init-only syntax = get + init [OK]
- Using 'set' instead of 'init'
- Trying to use 'readonly' in property accessor
- Confusing 'private set' with init-only
public class Person {
public string Name { get; init; }
}
var p = new Person { Name = "Alice" };
p.Name = "Bob";
Console.WriteLine(p.Name);Solution
Step 1: Understand init-only property behavior
The propertyNamecan only be set during object initialization (inside the braces). Setting it later causes a compile error.Step 2: Analyze the code
After creatingpwithName = "Alice", the code tries to assignp.Name = "Bob"outside initialization, which is invalid.Final Answer:
Compile-time error -> Option DQuick Check:
Changing init-only after creation = compile error [OK]
- Assuming property can be changed anytime
- Expecting runtime error instead of compile error
- Confusing init-only with readonly fields
public class Car {
public string Model { get; init; }
}
var car = new Car();
car.Model = "Sedan";
Solution
Step 1: Check how init-only properties are set
Init-only properties must be set during object creation using an initializer, not after.Step 2: Analyze the code
The code createscarwith default constructor, then tries to setModelproperty outside initialization, which is invalid.Final Answer:
Cannot assign init-only property outside object initializer -> Option CQuick Check:
Init-only set only in initializer, not later [OK]
- Trying to set init-only property after creation
- Thinking init-only means readonly
- Ignoring object initializer syntax
public record Book {
public string Title { get; init; }
public string Author { get; init; }
}
var book1 = new Book { Title = "C# Guide", Author = "Jane" };
var book2 = book1 with { Author = "John" };
Console.WriteLine(book2.Title + ", " + book2.Author);What is the output?
Solution
Step 1: Understand 'with' expression on records
The 'with' expression creates a copy ofbook1but allows changing init-only properties during creation of the new object.Step 2: Analyze the output
book2copiesTitleas "C# Guide" and setsAuthorto "John". Printing shows "C# Guide, John".Final Answer:
C# Guide, John -> Option AQuick Check:
'with' allows changing init-only during copy [OK]
- Thinking 'with' cannot change init-only properties
- Expecting compile error on 'with' usage
- Confusing init-only with immutable fields
