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

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

Choose your learning style9 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.