A record struct is a value type that provides built-in features like immutability, value equality, and concise syntax for defining data containers, introduced in C# 10.
Click to reveal answer
intermediate
How does a record struct differ from a regular struct?
A record struct automatically provides value-based equality, a concise syntax for defining properties, and supports with-expressions for copying with modifications, unlike regular structs which require manual implementation.
Click to reveal answer
intermediate
Can a record struct be mutable or immutable?
By default, record structs are mutable, but you can make them immutable by declaring properties with only getters or using the readonly modifier.
Click to reveal answer
beginner
What feature allows you to create a copy of a record struct with some properties changed?
The with expression lets you create a new record struct by copying an existing one and changing specified properties.
Click to reveal answer
beginner
Write a simple example of a record struct with two properties: int X and int Y.
public record struct Point(int X, int Y);
Click to reveal answer
Which of the following is true about record structs in C#?
AThey are reference types with value equality.
BThey are value types with built-in value equality.
CThey cannot have properties.
DThey do not support the with-expression.
✗ Incorrect
Record structs are value types that provide built-in value equality and support with-expressions.
How do you declare a record struct named 'Point' with properties 'X' and 'Y'?
Astruct Point record(int X, int Y);
Bstruct record Point { int X; int Y; }
Crecord Point struct(int X, int Y);
Drecord struct Point(int X, int Y);
✗ Incorrect
The correct syntax is 'record struct Point(int X, int Y);' which declares a record struct with two properties.
What does the 'with' expression do for record structs?
ACreates a copy with some properties changed.
BDeletes the record struct instance.
CConverts the record struct to a class.
DMakes the record struct mutable.
✗ Incorrect
The 'with' expression creates a new copy of the record struct with specified properties changed.
Are record structs mutable by default?
ANo, they are immutable by default.
BThey cannot be mutable.
CYes, they are mutable by default.
DThey are always readonly.
✗ Incorrect
Record structs are mutable by default unless marked readonly or with immutable properties.
Which C# version introduced record structs?
AC# 10
BC# 8
CC# 9
DC# 7
✗ Incorrect
Record structs were introduced in C# 10.
Explain what a record struct is and how it differs from a regular struct in C#.
Think about how record structs help with comparing and copying data.
You got /5 concepts.
Describe how you can create a copy of a record struct with one property changed.
Remember the keyword that starts with 'w' used for copying with changes.
You got /3 concepts.
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
Step 1: Understand record struct purpose
Record structs combine struct's lightweight nature with record's value-based equality and immutability.
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.
Final Answer:
It provides value-based equality and immutability by default. -> Option A
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
Step 1: Recall record struct declaration syntax
The concise syntax for record structs uses positional parameters: record struct Name(type prop1, type prop2);
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.
Final Answer:
record struct Point(int X, int Y); -> Option C
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
Step 1: Understand equality in record structs
Record structs provide value-based equality by default, so two instances with same values are equal.
Step 2: Analyze the code output
Both p1 and p2 have X=3 and Y=4, so p1 == p2 evaluates to true.
Final Answer:
True -> Option A
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
Step 1: Understand immutability of record structs
Record structs with positional parameters generate readonly properties by default.
Step 2: Analyze the assignment line
Trying to assign rect.Width = 7; causes a compile-time error because Width is readonly.
Final Answer:
Cannot assign to 'Width' because it is readonly. -> Option D
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
Step 1: Identify requirements
Needs lightweight, immutable, value equality, and nice printing.
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.
Final Answer:
record struct Point3D(int X, int Y, int Z); -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.