Init-only setters in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time it takes to set properties using init-only setters changes as we create more objects.
We want to know how the cost grows when initializing many objects with these setters.
Analyze the time complexity of the following code snippet.
public record Person
{
public string Name { get; init; }
public int Age { get; init; }
}
var people = new List();
for (int i = 0; i < n; i++)
{
people.Add(new Person { Name = $"Person{i}", Age = i });
}
This code creates a list of Person objects, each initialized with a name and age using init-only setters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and initializing a Person object inside a loop.
- How many times: The loop runs n times, once for each Person created.
Each new object requires a fixed amount of work to set its properties.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 object creations and initializations |
| 100 | About 100 object creations and initializations |
| 1000 | About 1000 object creations and initializations |
Pattern observation: The work grows directly with the number of objects created.
Time Complexity: O(n)
This means the time to initialize all objects grows linearly as you create more objects.
[X] Wrong: "Init-only setters make object creation instant regardless of how many objects."
[OK] Correct: Each object still needs its properties set one by one, so more objects mean more work.
Understanding how object initialization scales helps you write efficient code and explain your reasoning clearly in interviews.
"What if we replaced the init-only setters with regular setters and modified properties after creation? How would the time complexity change?"
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
