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

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

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