With expressions for immutable copies in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to create immutable copies using with expressions changes as the data size grows.
Specifically, how does copying an object with some changes affect performance when the object has many properties?
Analyze the time complexity of the following code snippet.
record Person(string Name, int Age, string Address);
var original = new Person("Alice", 30, "123 Main St");
var copy = original with { Age = 31 };
This code creates a new immutable copy of a Person record, changing only the Age property.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying each property from the original object to the new one.
- How many times: Once per property in the record.
As the number of properties in the record grows, the time to create a copy grows proportionally.
| Input Size (number of properties) | Approx. Operations |
|---|---|
| 3 | 3 property copies |
| 10 | 10 property copies |
| 100 | 100 property copies |
Pattern observation: The time grows linearly with the number of properties copied.
Time Complexity: O(n)
This means the time to create a copy grows in direct proportion to the number of properties in the object.
[X] Wrong: "Creating a copy with a with expression is instant no matter how big the object is."
[OK] Correct: Each property must be copied to the new object, so more properties mean more work and more time.
Understanding how copying objects scales helps you write efficient code and explain your choices clearly in interviews.
"What if the record contains nested records or collections? How would the time complexity change?"
Practice
with expression do in C# when used with records?Solution
Step 1: Understand the purpose of
Thewithexpressionwithexpression is designed to create a new object by copying an existing one but allows changing some properties.Step 2: Compare behavior with original object
The original object remains unchanged; only the new object has the updated properties.Final Answer:
Creates a new record by copying an existing one and changing specified properties -> Option AQuick Check:
With expression = new copy with changes [OK]
- Thinking with modifies the original object
- Assuming with deletes the original object
- Believing with copies without allowing changes
person2 from person1 with a changed Age property using a with expression?Solution
Step 1: Recall the correct syntax for with expressions
The correct syntax is:var newRecord = oldRecord with { Property = value };Step 2: Match the syntax with options
var person2 = person1 with { Age = 30 }; matches the correct syntax exactly, others have misplaced keywords or incorrect order.Final Answer:
var person2 = person1 with { Age = 30 }; -> Option DQuick Check:
Correct syntax = var person2 = person1 with { Age = 30 }; [OK]
- Placing 'with' before the variable
- Using incorrect braces or order
- Trying to assign property outside braces
public record Person(string Name, int Age);
var person1 = new Person("Alice", 25);
var person2 = person1 with { Age = 30 };
Console.WriteLine(person1.Age);
Console.WriteLine(person2.Age);Solution
Step 1: Understand the with expression effect on Age
person2is a new record copied fromperson1but withAgechanged to 30.person1remains unchanged withAge25.Step 2: Determine the printed values
Printingperson1.Ageoutputs 25, printingperson2.Ageoutputs 30.Final Answer:
25 30 -> Option CQuick Check:
Original Age = 25, New Age = 30 [OK]
- Assuming original object changes
- Mixing up which object's property is printed first
- Expecting both to have same Age
public record Point(int X, int Y); var p1 = new Point(5, 10); var p2 = p1 with (X = 7);
Solution
Step 1: Check syntax of with expression
The with expression requires braces{ }around property assignments, not parentheses( ).Step 2: Identify the error in code
Code useswith (X = 7)which is invalid syntax; it should bewith { X = 7 }.Final Answer:
Using parentheses instead of braces in with expression -> Option AQuick Check:
with uses braces, not parentheses [OK]
- Using parentheses instead of braces
- Thinking with can't change properties
- Assuming with is not allowed on records
public record Address(string City, string Country);
public record Person(string Name, int Age, Address Address);
var addr1 = new Address("Paris", "France");
var person1 = new Person("Bob", 40, addr1);
var person2 = person1 with { Address = person1.Address with { City = "Lyon" } };
Console.WriteLine(person1.Address.City);
Console.WriteLine(person2.Address.City);Solution
Step 1: Understand nested with expressions
person2is a newPersoncopied fromperson1, but itsAddressproperty is also copied and changed with a newCityvalue "Lyon".Step 2: Determine original and new city values
person1.Address.Cityremains "Paris" because original objects are immutable.person2.Address.Cityis "Lyon" due to the nested with expression.Final Answer:
Paris Lyon -> Option BQuick Check:
Nested with copies inner object with changes [OK]
- Assuming inner object changes original
- Expecting both cities to be same
- Not using nested with for inner property change
Ready to prove it?
Pass the test and this pattern moves unknown
