Bird
Raised Fist0
C Sharp (C#)programming~20 mins

With expressions for immutable copies in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
With Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple with expression on a record
What is the output of this C# code using a with expression on a record?
C Sharp (C#)
public record Person(string Name, int Age);

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

System.Console.WriteLine(p2);
APerson { Name = Alice, Age = 31 }
BPerson { Name = Alice, Age = 30 }
CPerson { Name = , Age = 31 }
DCompilation error
Attempts:
2 left
💡 Hint
The with expression creates a new record copying all properties except those changed.
❓ Predict Output
intermediate
2:00remaining
Effect of with expression on nested records
Given these nested records, what is the output of the code below?
C Sharp (C#)
public record Address(string City, string Country);
public record Employee(string Name, Address Address);

var e1 = new Employee("Bob", new Address("Paris", "France"));
var e2 = e1 with { Address = e1.Address with { City = "Lyon" } };

System.Console.WriteLine(e2.Address.City);
ACompilation error
BParis
CFrance
DLyon
Attempts:
2 left
💡 Hint
With expressions can be nested to create copies with changes in nested properties.
❓ Predict Output
advanced
2:00remaining
Output when using with expression on a class instead of a record
What happens when you try to use a with expression on a class instead of a record in C#?
C Sharp (C#)
public class Car { public string Model { get; init; } public int Year { get; init; } }

var c1 = new Car { Model = "Tesla", Year = 2020 };
var c2 = c1 with { Year = 2021 };

System.Console.WriteLine(c2.Year);
ACompilation error
B2020
C2021
DRuntime error
Attempts:
2 left
💡 Hint
With expressions only work on records by default.
❓ Predict Output
advanced
2:00remaining
Output of with expression with positional record and property change
What is the output of this code using a positional record and a with expression changing a property?
C Sharp (C#)
public record Point(int X, int Y);

var p1 = new Point(5, 10);
var p2 = p1 with { Y = 20 };

System.Console.WriteLine($"X={p2.X}, Y={p2.Y}");
AX=5, Y=10
BX=5, Y=20
CX=0, Y=20
DCompilation error
Attempts:
2 left
💡 Hint
With expressions copy all properties except those changed.
❓ Predict Output
expert
3:00remaining
Output of with expression with inheritance and init-only properties
Consider these records with inheritance. What is the output of the code below?
C Sharp (C#)
public record Animal(string Name);
public record Dog(string Name, string Breed) : Animal(Name);

var d1 = new Dog("Buddy", "Beagle");
var d2 = d1 with { Breed = "Labrador" };

System.Console.WriteLine($"Name: {d2.Name}, Breed: {d2.Breed}");
AName: Buddy, Breed: Beagle
BName: , Breed: Labrador
CName: Buddy, Breed: Labrador
DCompilation error
Attempts:
2 left
💡 Hint
With expressions work with inheritance and copy base properties automatically.

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

  1. 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.
  2. Step 2: Compare behavior with original object

    The original object remains unchanged; only the new object has the updated properties.
  3. Final Answer:

    Creates a new record by copying an existing one and changing specified properties -> Option A
  4. 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

  1. Step 1: Recall the correct syntax for with expressions

    The correct syntax is: var newRecord = oldRecord with { Property = value };
  2. 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.
  3. Final Answer:

    var person2 = person1 with { Age = 30 }; -> Option D
  4. 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

  1. 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.
  2. Step 2: Determine the printed values

    Printing person1.Age outputs 25, printing person2.Age outputs 30.
  3. Final Answer:

    25 30 -> Option C
  4. Quick Check:

    Original Age = 25, New Age = 30 [OK]
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

  1. Step 1: Check syntax of with expression

    The with expression requires braces { } around property assignments, not parentheses ( ).
  2. Step 2: Identify the error in code

    Code uses with (X = 7) which is invalid syntax; it should be with { X = 7 }.
  3. Final Answer:

    Using parentheses instead of braces in with expression -> Option A
  4. 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

  1. 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".
  2. 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.
  3. Final Answer:

    Paris Lyon -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard