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

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

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

Practice

(1/5)
1. What is a key feature of record struct in C#?
easy
A. It provides value-based equality and immutability by default.
B. It allows inheritance from other structs.
C. It can only be used for reference types.
D. It does not support property declarations.

Solution

  1. Step 1: Understand record struct purpose

    Record structs combine struct's lightweight nature with record's value-based equality and immutability.
  2. Step 2: Analyze options

    It provides value-based equality and immutability by default. correctly states value-based equality and immutability. Options B, C, and D are incorrect because structs cannot inherit, record structs are value types, and they support properties.
  3. Final Answer:

    It provides value-based equality and immutability by default. -> Option A
  4. Quick Check:

    record struct = value equality + immutability [OK]
Hint: Remember: record structs behave like immutable value containers [OK]
Common Mistakes:
  • Thinking record structs support inheritance
  • Confusing record structs with classes
  • Assuming record structs are reference types
2. Which of the following is the correct syntax to declare a record struct named Point with two properties X and Y of type int?
easy
A. struct record Point { int X; int Y; }
B. record Point struct { int X; int Y; }
C. record struct Point(int X, int Y);
D. record struct Point { int X; int Y; }

Solution

  1. Step 1: Recall record struct declaration syntax

    The concise syntax for record structs uses positional parameters: record struct Name(type prop1, type prop2);
  2. Step 2: Compare options

    record struct Point(int X, int Y); matches the correct syntax. record struct Point { int X; int Y; } is close but missing parentheses for positional parameters. Options A and C have incorrect order or keywords.
  3. Final Answer:

    record struct Point(int X, int Y); -> Option C
  4. Quick Check:

    record struct with positional params = record struct Point(int X, int Y); [OK]
Hint: Use parentheses for positional properties in record structs [OK]
Common Mistakes:
  • Swapping 'record' and 'struct' keywords
  • Omitting parentheses for positional parameters
  • Using braces instead of parentheses for positional syntax
3. What will be the output of the following code?
record struct Point(int X, int Y);
var p1 = new Point(3, 4);
var p2 = new Point(3, 4);
Console.WriteLine(p1 == p2);
medium
A. True
B. False
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand equality in record structs

    Record structs provide value-based equality by default, so two instances with same values are equal.
  2. Step 2: Analyze the code output

    Both p1 and p2 have X=3 and Y=4, so p1 == p2 evaluates to true.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    record struct equality = value equality [OK]
Hint: Record structs compare values, not references [OK]
Common Mistakes:
  • Assuming == compares references for structs
  • Expecting compilation error for == operator
  • Confusing record structs with classes for equality
4. Identify the error in this code snippet:
record struct Rectangle(int Width, int Height);
var rect = new Rectangle(5, 10);
rect.Width = 7;
Console.WriteLine(rect);
medium
A. Rectangle must be a class to allow property assignment.
B. Missing semicolon after record struct declaration.
C. Console.WriteLine cannot print record structs.
D. Cannot assign to 'Width' because it is readonly.

Solution

  1. Step 1: Understand immutability of record structs

    Record structs with positional parameters generate readonly properties by default.
  2. Step 2: Analyze the assignment line

    Trying to assign rect.Width = 7; causes a compile-time error because Width is readonly.
  3. Final Answer:

    Cannot assign to 'Width' because it is readonly. -> Option D
  4. Quick Check:

    Positional record struct properties are readonly [OK]
Hint: Positional record struct properties are readonly by default [OK]
Common Mistakes:
  • Trying to modify readonly properties
  • Confusing record structs with mutable classes
  • Ignoring compiler error messages
5. You want to create a lightweight data type to store a 3D point with coordinates X, Y, and Z. You want it to be immutable, support value equality, and print nicely. Which is the best way to declare it using record structs?
hard
A. struct Point3D { public int X; public int Y; public int Z; }
B. record struct Point3D(int X, int Y, int Z);
C. class Point3D { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } }
D. record class Point3D(int X, int Y, int Z);

Solution

  1. Step 1: Identify requirements

    Needs lightweight, immutable, value equality, and nice printing.
  2. Step 2: Match requirements to options

    record struct Point3D(int X, int Y, int Z); uses record struct with positional parameters, providing all requirements. struct Point3D { public int X; public int Y; public int Z; } is mutable struct without value equality or nice printing. class Point3D { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } } is mutable class, heavier and reference type. record class Point3D(int X, int Y, int Z); is record class, reference type, not lightweight.
  3. Final Answer:

    record struct Point3D(int X, int Y, int Z); -> Option B
  4. Quick Check:

    record struct = lightweight + immutable + value equality [OK]
Hint: Use record struct with positional params for immutable lightweight data [OK]
Common Mistakes:
  • Choosing class instead of struct for lightweight data
  • Using mutable fields instead of readonly properties
  • Confusing record class with record struct
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