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

Value equality in records in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Value equality in records
Create record instance A
Create record instance B
Compare A and B
Return true
This flow shows how two record instances are created and compared by their values, returning true if all properties match.
Execution Sample
C Sharp (C#)
public record Person(string Name, int Age);

var p1 = new Person("Alice", 30);
var p2 = new Person("Alice", 30);

bool areEqual = p1 == p2;
Console.WriteLine(areEqual);
This code creates two Person records with the same data and compares them for value equality, printing the result.
Execution Table
StepActionEvaluationResult
1Create p1 with Name="Alice", Age=30p1 = Person(Name="Alice", Age=30)p1 created
2Create p2 with Name="Alice", Age=30p2 = Person(Name="Alice", Age=30)p2 created
3Compare p1 == p2Compare all properties: Name and AgeTrue (values equal)
4Print areEqualOutput value of areEqualTrue
💡 Comparison ends after checking all properties are equal, so result is True.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
p1nullPerson(Name="Alice", Age=30)Person(Name="Alice", Age=30)Person(Name="Alice", Age=30)Person(Name="Alice", Age=30)
p2nullnullPerson(Name="Alice", Age=30)Person(Name="Alice", Age=30)Person(Name="Alice", Age=30)
areEqualundefinedundefinedundefinedTrueTrue
Key Moments - 2 Insights
Why does p1 == p2 return true even though they are different objects?
Because records in C# compare their contents (values of properties) for equality, not their memory addresses. See execution_table step 3 where all properties are compared.
What if one property differs between p1 and p2?
Then the comparison returns false because value equality requires all properties to match exactly. This is shown in execution_table step 3 where all properties must be equal.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the result of comparing p1 and p2?
AThrows error
BFalse
CTrue
DNull
💡 Hint
Check the 'Result' column in step 3 of execution_table.
According to variable_tracker, what is the value of areEqual after step 3?
AFalse
BTrue
Cnull
Dundefined
💡 Hint
Look at the 'areEqual' row under 'After Step 3' in variable_tracker.
If p2 was created with Age=31 instead of 30, what would be the comparison result?
AFalse
BTrue
CThrows exception
DComparison skipped
💡 Hint
Value equality requires all properties to match exactly, see key_moments explanation.
Concept Snapshot
C# records compare by value, not reference.
Use '==' to check if all properties are equal.
Records auto-generate equality methods.
Different objects with same data are equal.
If any property differs, equality is false.
Full Transcript
This example shows how C# records compare two instances by their values. Two Person records with the same Name and Age are created. When compared with '==', the result is true because records check if all properties match. The variable 'areEqual' stores this result and prints true. This demonstrates value equality in records, meaning objects with identical data are considered equal even if they are different instances.