Challenge - 5 Problems
Record Structs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of record struct with property init
What is the output of this C# code snippet using a record struct?
C Sharp (C#)
public record struct Point(int X, int Y); var p1 = new Point(3, 4); var p2 = p1 with { Y = 10 }; Console.WriteLine(p2);
Attempts:
2 left
💡 Hint
Remember that record structs support 'with' expressions to create copies with modifications.
✗ Incorrect
The 'with' expression creates a copy of the record struct with the specified property changed. Here, Y is changed to 10 while X remains 3.
🧠 Conceptual
intermediate1:30remaining
Immutability of record structs
Which statement about record structs in C# is true?
Attempts:
2 left
💡 Hint
Think about how record structs differ from classes and normal structs.
✗ Incorrect
Record structs provide value equality and can have mutable properties. They are value types, so they differ from classes in reference behavior.
🔧 Debug
advanced2:30remaining
Fix the error in record struct declaration
This code produces a compilation error. What is the cause?
C Sharp (C#)
public record struct Rectangle { public int Width { get; init; } public int Height { get; init; } public Rectangle(int width, int height) : this() { Width = width; Height = height; } }
Attempts:
2 left
💡 Hint
In structs, constructors must initialize all fields and call the parameterless constructor if present.
✗ Incorrect
In C#, struct constructors must call the parameterless constructor (or chain to another constructor) before assigning to properties. Omitting 'this()' causes a compilation error.
📝 Syntax
advanced1:30remaining
Identify the invalid record struct syntax
Which of the following record struct declarations is invalid in C#?
Attempts:
2 left
💡 Hint
Consider inheritance rules for structs in C#.
✗ Incorrect
Structs, including record structs, cannot inherit from other structs or classes, so ': BaseStruct' is invalid syntax.
🚀 Application
expert2:00remaining
Value equality behavior of record structs
Given this code, what is the output?
C Sharp (C#)
public record struct Coordinate(int X, int Y); var c1 = new Coordinate(5, 7); var c2 = new Coordinate(5, 7); Console.WriteLine(c1 == c2); Console.WriteLine(ReferenceEquals(c1, c2));
Attempts:
2 left
💡 Hint
Record structs provide value equality but are value types, so reference equality behaves differently.
✗ Incorrect
The '==' operator for record structs compares values and returns true. ReferenceEquals returns false because structs are value types and boxed separately.