Init-only setters let you set a property only when creating an object. After that, the property cannot change. This helps keep data safe and clear.
Init-only setters in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
public class Person { public string Name { get; init; } public int Age { get; init; } }
The init keyword allows setting the property only during object creation.
After the object is created, trying to change the property will cause a compile error.
Examples
C Sharp (C#)
var person = new Person { Name = "Alice", Age = 30 };
C Sharp (C#)
person.Name = "Bob"; // This will cause a compile error
C Sharp (C#)
public record Car { public string Model { get; init; } public int Year { get; init; } }
Sample Program
This program creates a Person object with init-only properties. It prints the values. Trying to change Age after creation is not allowed.
C Sharp (C#)
using System; public class Person { public string Name { get; init; } public int Age { get; init; } } class Program { static void Main() { var person = new Person { Name = "Alice", Age = 30 }; Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); // person.Age = 31; // This line would cause a compile error } }
Important Notes
Init-only setters were introduced in C# 9.0.
You can only set init-only properties during object initialization or in the constructor.
Summary
Init-only setters let you set properties only when creating an object.
They help make objects immutable after creation.
Trying to change an init-only property later causes a compile error.
Practice
1. What is the main purpose of
init-only setters in C#?easy
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]
Hint: Init-only means set once during creation only [OK]
Common Mistakes:
- Thinking init-only allows changes anytime
- Confusing init-only with readonly fields
- Assuming init-only affects methods
2. Which of the following is the correct syntax to declare an init-only property in C#?
easy
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]
Hint: Init-only uses 'init' keyword, not 'set' [OK]
Common Mistakes:
- Using 'set' instead of 'init'
- Trying to use 'readonly' in property accessor
- Confusing 'private set' with init-only
3. What will happen when you run this code?
public class Person {
public string Name { get; init; }
}
var p = new Person { Name = "Alice" };
p.Name = "Bob";
Console.WriteLine(p.Name);medium
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]
Hint: Init-only properties can't be changed after creation [OK]
Common Mistakes:
- Assuming property can be changed anytime
- Expecting runtime error instead of compile error
- Confusing init-only with readonly fields
4. Identify the error in this code snippet:
public class Car {
public string Model { get; init; }
}
var car = new Car();
car.Model = "Sedan";
medium
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]
Hint: Set init-only properties inside braces only [OK]
Common Mistakes:
- Trying to set init-only property after creation
- Thinking init-only means readonly
- Ignoring object initializer syntax
5. Given this record declaration:
What is the output?
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?
hard
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]
Hint: 'with' can set init-only properties on new record copies [OK]
Common Mistakes:
- Thinking 'with' cannot change init-only properties
- Expecting compile error on 'with' usage
- Confusing init-only with immutable fields
