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

With expressions for immutable copies in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - With expressions for immutable copies
Create original record
Use 'with' expression
Copy original with changes
New record created, original unchanged
Use new record as needed
Start with an original record, use 'with' to make a copy with some changes, resulting in a new record while the original stays the same.
Execution Sample
C Sharp (C#)
record Person(string Name, int Age);

var p1 = new Person("Alice", 30);
var p2 = p1 with { Age = 31 };

Console.WriteLine(p1);
Console.WriteLine(p2);
Creates a Person record, then makes a copy with a changed Age using 'with' expression, printing both.
Execution Table
StepActionVariable ValuesOutput
1Create p1 with Name='Alice', Age=30p1 = Person(Name='Alice', Age=30)
2Create p2 as copy of p1 with Age=31p2 = Person(Name='Alice', Age=31)
3Print p1p1 unchangedPerson { Name = Alice, Age = 30 }
4Print p2p2 has Age=31Person { Name = Alice, Age = 31 }
5EndNo changes
💡 Execution ends after printing both records; original remains unchanged.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
p1undefinedPerson(Name='Alice', Age=30)Person(Name='Alice', Age=30)Person(Name='Alice', Age=30)
p2undefinedundefinedPerson(Name='Alice', Age=31)Person(Name='Alice', Age=31)
Key Moments - 2 Insights
Why does changing Age in p2 not affect p1?
Because 'with' creates a new copy with changes, leaving the original p1 unchanged, as shown in steps 2 and 3 of the execution_table.
Is p2 a reference to p1 or a new object?
p2 is a new object created by copying p1 and changing Age, not a reference to p1, as seen in variable_tracker after step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the Age value of p2?
A30
Bundefined
C31
D0
💡 Hint
Check the 'Variable Values' column at step 2 in execution_table.
At which step does p1 get created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for creation of p1.
If we changed p1's Name after step 1, would p2's Name change?
ANo, because p2 is a copy made before the change
BYes, because records are mutable
CYes, because p2 references p1
DNo, because p2 is unrelated
💡 Hint
Refer to variable_tracker and key_moments about immutability and copying.
Concept Snapshot
With expressions create a new immutable copy of a record with some changes.
Syntax: var newRecord = oldRecord with { Property = newValue };
Original record stays unchanged.
Useful for immutable data updates.
Works only with records or types supporting 'with'.
Full Transcript
This example shows how to use 'with' expressions in C# to create immutable copies of records. First, we create a record p1 with Name 'Alice' and Age 30. Then, using 'with', we create p2 as a copy of p1 but with Age changed to 31. Printing p1 and p2 shows that p1 remains unchanged while p2 has the updated Age. This demonstrates that 'with' expressions produce new objects without modifying the original, supporting safe immutable data handling.