0
0
C Sharp (C#)programming~10 mins

Init-only setters in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Init-only setters
Create object instance
Set properties with init-only setters
Object is immutable after initialization
Attempt to modify property after init -> Compile error
This flow shows how init-only setters allow setting properties only during object creation, making the object immutable afterward.
Execution Sample
C Sharp (C#)
public record Person
{
    public string Name { get; init; }
}

var p = new Person { Name = "Alice" };
This code creates a Person object and sets the Name property using an init-only setter during initialization.
Execution Table
StepActionProperty NameValue SetResult
1Create Person object--Object created with default values
2Set Name during initializationName"Alice"Name property set to "Alice"
3Attempt to modify Name after initializationName"Bob"Compile-time error: Cannot assign to 'Name' because it is init-only
💡 Execution stops because modifying init-only property after initialization causes a compile-time error.
Variable Tracker
VariableStartAfter InitializationAfter Attempted Modification
p.Namenull"Alice"Compile error - no change
Key Moments - 2 Insights
Why can't I change the property value after the object is created?
Because the property uses an init-only setter, it can only be set during object initialization. See execution_table step 3 where modifying it later causes a compile error.
Can I set the property inside the constructor?
Yes, init-only setters allow setting properties inside the constructor or during object initialization, as shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p.Name after initialization?
A"Bob"
B"Alice"
Cnull
DCompile error
💡 Hint
Check execution_table row 2 where Name is set during initialization.
At which step does the compile-time error occur?
AStep 3
BStep 2
CStep 1
DNo error occurs
💡 Hint
See execution_table row 3 where modifying the property after initialization causes error.
If the setter was not init-only, what would happen at step 3?
ACompile-time error
BRuntime exception
CProperty value changes to "Bob"
DProperty remains "Alice"
💡 Hint
Without init-only, properties can be changed anytime, so step 3 would succeed.
Concept Snapshot
Init-only setters allow properties to be set only during object creation.
Syntax: public string Name { get; init; }
After initialization, properties become immutable.
Trying to set them later causes compile-time errors.
Useful for immutable data models with flexible initialization.
Full Transcript
Init-only setters in C# let you set properties only when creating an object. After the object is made, you cannot change those properties. This makes the object immutable after initialization. For example, a Person record with a Name property using init-only setter can have Name set during creation but not changed later. Trying to change it after creation causes a compile-time error. This helps keep data safe and consistent.