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

Record structs in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Record structs
Define record struct
↓
Create instance with values
↓
Use instance: access properties
↓
Compare instances (value equality)
↓
Immutable by default, can use with-expressions
↓
End
This flow shows how a record struct is defined, instantiated, used, compared, and how its immutability works.
Execution Sample
C Sharp (C#)
public record struct Point(int X, int Y);

var p1 = new Point(3, 4);
var p2 = new Point(3, 4);

Console.WriteLine(p1 == p2);
Defines a record struct Point, creates two instances with same values, and compares them for equality.
Execution Table
StepActionEvaluationResult
1Define record struct Point with properties X and YN/APoint type created
2Create p1 = new Point(3, 4)X=3, Y=4p1 holds (3,4)
3Create p2 = new Point(3, 4)X=3, Y=4p2 holds (3,4)
4Compare p1 == p2Compare values of X and YTrue
5Print resultOutput booleanTrue
💡 Comparison done, program ends after printing True
Variable Tracker
VariableStartAfter Step 2After Step 3Final
p1undefined(3,4)(3,4)(3,4)
p2undefinedundefined(3,4)(3,4)
Key Moments - 3 Insights
Why does p1 == p2 return True even though they are different instances?
Record structs compare their contents (values) for equality, not their memory addresses. See execution_table step 4 where values (3,4) are compared.
Are record structs mutable like classes?
No, record structs are immutable by default, meaning their properties cannot be changed after creation unless explicitly declared mutable.
What happens if we try to change a property of p1 after creation?
Since properties are readonly by default, the compiler will give an error. You must create a new instance with desired changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the result of comparing p1 and p2?
AFalse
BTrue
CCompilation error
DRuntime exception
💡 Hint
Check the 'Result' column in execution_table row 4.
According to variable_tracker, what is the value of p2 after step 3?
A(0,0)
Bundefined
C(3,4)
Dnull
💡 Hint
Look at variable_tracker row for p2 under 'After Step 3'.
If we try to assign p1.X = 5 after creation, what will happen?
ACompilation error due to immutability
BRuntime exception
CThe value of p1.X changes to 5
Dp1 becomes null
💡 Hint
Refer to key_moments about immutability and property changes.
Concept Snapshot
Record structs are value types with immutable properties by default.
They support value-based equality (== compares contents).
Defined with 'record struct' syntax.
Instances are created with positional parameters.
Properties cannot be changed after creation unless mutable.
Use with-expressions to create modified copies.
Full Transcript
This example shows how to define a record struct named Point with two properties X and Y. We create two instances p1 and p2 with the same values (3,4). When we compare p1 and p2 using ==, the result is True because record structs compare their values, not references. The variables p1 and p2 hold the values (3,4) after creation. Record structs are immutable by default, so trying to change a property after creation causes a compilation error. This behavior ensures safe and predictable value semantics.

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