A 'with expression' creates a new object by copying an existing immutable object and changing some of its properties without modifying the original.
Click to reveal answer
beginner
How do 'with expressions' help with immutability?
They allow you to create modified copies of objects without changing the original, preserving immutability and avoiding side effects.
Click to reveal answer
beginner
Given a record:
record Person(string Name, int Age);
How do you create a copy with a new Age using a with expression?
var olderPerson = person with { Age = person.Age + 1 };
This creates a new Person with the same Name but Age increased by 1.
Click to reveal answer
intermediate
Can 'with expressions' be used with classes that are not records?
No, 'with expressions' work primarily with records or types that support them. Regular classes need custom cloning methods.
Click to reveal answer
intermediate
What happens if you use a with expression but don't change any properties?
A new object is still created, but it has the same property values as the original, effectively a shallow copy.
Click to reveal answer
What does the 'with' keyword do in C# records?
AConverts a record to a class
BModifies the original record in place
CDeletes the record
DCreates a new record by copying and modifying specified properties
✗ Incorrect
The 'with' keyword creates a new record by copying the original and changing specified properties.
Which of these is true about 'with expressions'?
AThey create a new object with some changed properties
BThey are used to delete properties
CThey only work with mutable classes
DThey mutate the original object
✗ Incorrect
'With expressions' create a new object with some properties changed, leaving the original unchanged.
Given
record Point(int X, int Y);
How to create a new Point with X=5 but same Y as p?
ABoth B and C
Bvar p2 = new Point(5, p.Y);
Cvar p2 = p with { X = 5 };
DNone of the above
✗ Incorrect
Both ways create a new Point with X=5 and Y same as p.Y.
Can you use 'with expressions' to change nested objects inside a record?
AYes, directly and deeply
BNo, only top-level properties can be changed
CYes, but only if nested objects are mutable
DNo, 'with' only works on classes
✗ Incorrect
'With expressions' only create shallow copies and change top-level properties.
What C# feature introduced 'with expressions' for immutable copies?
AC# 7.0
BC# 8.0
CC# 9.0
DC# 10.0
✗ Incorrect
'With expressions' were introduced in C# 9.0 along with records.
Explain how 'with expressions' help maintain immutability in C# records.
Think about how you can change data without changing the original object.
You got /3 concepts.
Describe the difference between using a 'with expression' and modifying an object directly.
Consider what happens to the original object in each case.
You got /3 concepts.
Practice
(1/5)
1. What does the with expression do in C# when used with records?
easy
A. Creates a new record by copying an existing one and changing specified properties
B. Modifies the original record directly
C. Deletes the original record and creates a new one
D. Copies the record without allowing any changes
Solution
Step 1: Understand the purpose of with expression
The with expression 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 A
Quick Check:
With expression = new copy with changes [OK]
Hint: Remember: with creates a new object, original stays same [OK]
Common Mistakes:
Thinking with modifies the original object
Assuming with deletes the original object
Believing with copies without allowing changes
2. Which of the following is the correct syntax to create a new record person2 from person1 with a changed Age property using a with expression?
easy
A. var person2 = person1.Age = 30 with;
B. var person2 = with person1 { Age = 30 };
C. var person2 = person1 { with Age = 30 };
D. var person2 = person1 with { Age = 30 };
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 D
Quick Check:
Correct syntax = var person2 = person1 with { Age = 30 }; [OK]
Hint: with comes after the variable, then braces with changes [OK]
Common Mistakes:
Placing 'with' before the variable
Using incorrect braces or order
Trying to assign property outside braces
3. Given the record declaration and code below, what is the output?
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);
medium
A. 25
25
B. 30
30
C. 25
30
D. 30
25
Solution
Step 1: Understand the with expression effect on Age
person2 is a new record copied from person1 but with Age changed to 30. person1 remains unchanged with Age 25.
Hint: Original stays same, new copy has changed property [OK]
Common Mistakes:
Assuming original object changes
Mixing up which object's property is printed first
Expecting both to have same Age
4. Identify the error in the following code using with expressions:
public record Point(int X, int Y);
var p1 = new Point(5, 10);
var p2 = p1 with (X = 7);
medium
A. Using parentheses instead of braces in with expression
B. Cannot use with expression on records
C. Missing semicolon after record declaration
D. Cannot change properties in with expression
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 uses with (X = 7) which is invalid syntax; it should be with { X = 7 }.
Final Answer:
Using parentheses instead of braces in with expression -> Option A
Quick Check:
with uses braces, not parentheses [OK]
Hint: Use braces { } after with, not parentheses ( ) [OK]
Common Mistakes:
Using parentheses instead of braces
Thinking with can't change properties
Assuming with is not allowed on records
5. Consider the following records and code. What will be the output?
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);
hard
A. Lyon
Lyon
B. Paris
Lyon
C. Paris
Paris
D. Lyon
Paris
Solution
Step 1: Understand nested with expressions
person2 is a new Person copied from person1, but its Address property is also copied and changed with a new City value "Lyon".
Step 2: Determine original and new city values
person1.Address.City remains "Paris" because original objects are immutable. person2.Address.City is "Lyon" due to the nested with expression.
Final Answer:
Paris
Lyon -> Option B
Quick Check:
Nested with copies inner object with changes [OK]
Hint: Use nested with to copy and change inner objects immutably [OK]
Common Mistakes:
Assuming inner object changes original
Expecting both cities to be same
Not using nested with for inner property change
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.