Records and Modern Types
Value Equality
Records compare their stored values, not just object identity.
value equality
Value equality means two objects are equal when their data is equal.
Value Equality
ValueEquality.cs
Replay: real traced execution (multi-file project)
using System;
record Point(int X, int Y)
{
public override string ToString()
{
return X + "," + Y;
}
}
class Program
{
static void Main()
{
int x = 3;
Point first = new Point(x, 4);
Point second = new Point(x, 4);
bool equal = first == second;
Console.WriteLine($"x={x}");
Console.WriteLine($"equal={equal}");
}
}
using System;
record Point(int X, int Y)
{
public override string ToString()
{
return X + "," + Y;
}
}
class Program
{
static void Main()
{
int x = 2;
Point first = new Point(x, 4);
Point second = new Point(x, 4);
bool equal = first == second;
Console.WriteLine($"x={x}");
Console.WriteLine($"equal={equal}");
}
}
using System;
record Point(int X, int Y)
{
public override string ToString()
{
return X + "," + Y;
}
}
class Program
{
static void Main()
{
int x = 5;
Point first = new Point(x, 4);
Point second = new Point(x, 4);
bool equal = first == second;
Console.WriteLine($"x={x}");
Console.WriteLine($"equal={equal}");
}
}
x ← 3, first ← 3,4, second ← 3,4, equal ← True
12{13 static void Main()14 {15 int x→ 3 = 3; //@x=2, 516 Point first→ 3,4 = new Point(x, 4);17 Point second→ 3,4 = new Point(x, 4);18 bool equal→ True = first3,4 == second3,4;1920 Console.WriteLine($"x={x3}");21 Console.WriteLine($"equal={equalTrue}");22 }outputx=3 equal=True
x ← 2, first ← 2,4, second ← 2,4, equal ← True
12{13 static void Main()14 {15 int x→ 2 = 2;16 Point first→ 2,4 = new Point(x, 4);17 Point second→ 2,4 = new Point(x, 4);18 bool equal→ True = first2,4 == second2,4;1920 Console.WriteLine($"x={x2}");21 Console.WriteLine($"equal={equalTrue}");22 }outputx=2 equal=True
x ← 5, first ← 5,4, second ← 5,4, equal ← True
12{13 static void Main()14 {15 int x→ 5 = 5;16 Point first→ 5,4 = new Point(x, 4);17 Point second→ 5,4 = new Point(x, 4);18 bool equal→ True = first5,4 == second5,4;1920 Console.WriteLine($"x={x5}");21 Console.WriteLine($"equal={equalTrue}");22 }outputx=5 equal=True