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

Record structs in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Record Structs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
APoint { X = 3, Y = 4 }
BPoint { X = 3, Y = 10 }
CCompilation error: 'with' expressions not supported on record structs
DPoint { X = 10, Y = 4 }
Attempts:
2 left
💡 Hint
Remember that record structs support 'with' expressions to create copies with modifications.
🧠 Conceptual
intermediate
1:30remaining
Immutability of record structs
Which statement about record structs in C# is true?
ARecord structs are always immutable and cannot have mutable properties.
BRecord structs behave exactly like classes in terms of reference equality.
CRecord structs can have mutable properties but still provide value equality semantics.
DRecord structs cannot have parameterized constructors.
Attempts:
2 left
💡 Hint
Think about how record structs differ from classes and normal structs.
🔧 Debug
advanced
2: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;
    }
}
AThe constructor must use 'this()' to call the parameterless constructor before assigning properties.
BRecord structs cannot have properties with 'init' accessors.
CThe properties must be declared as 'readonly' to be used in a record struct.
DThe constructor must be removed because record structs cannot have explicit constructors.
Attempts:
2 left
💡 Hint
In structs, constructors must initialize all fields and call the parameterless constructor if present.
📝 Syntax
advanced
1:30remaining
Identify the invalid record struct syntax
Which of the following record struct declarations is invalid in C#?
Apublic record struct Data(int X, int Y) : BaseStruct;
Bpublic record struct Car { public string Model { get; init; } public int Year { get; init; } }
Cpublic record struct Person(string Name, int Age);
Dpublic record struct Point { public int X; public int Y; }
Attempts:
2 left
💡 Hint
Consider inheritance rules for structs in C#.
🚀 Application
expert
2: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));
AFalse\nTrue
BFalse\nFalse
CTrue\nTrue
DTrue\nFalse
Attempts:
2 left
💡 Hint
Record structs provide value equality but are value types, so reference equality behaves differently.