Challenge - 5 Problems
With Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
The with expression creates a new record copying all properties except those changed.
✗ Incorrect
The with expression creates a new Person record copying all properties from p1 but sets Age to 31. So the output shows the updated Age.
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
With expressions can be nested to create copies with changes in nested properties.
✗ Incorrect
The code creates a new Employee e2 with a new Address where City is changed to Lyon. So printing e2.Address.City outputs Lyon.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
With expressions only work on records by default.
✗ Incorrect
With expressions are supported only on records. Using them on classes causes a compilation error.
❓ Predict Output
advanced2: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}");
Attempts:
2 left
💡 Hint
With expressions copy all properties except those changed.
✗ Incorrect
The with expression creates a new Point with X copied as 5 and Y changed to 20.
❓ Predict Output
expert3: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}");
Attempts:
2 left
💡 Hint
With expressions work with inheritance and copy base properties automatically.
✗ Incorrect
The with expression creates a new Dog copying Name from d1 and changing Breed to Labrador.