Records and Modern Types
Deconstruction
Records can be deconstructed into separate variables.
deconstruction
Deconstruction splits a value into named local variables.
Deconstruction
Deconstruction.cs
Replay: real traced execution (multi-file project)
using System;
record Pair(int Left, int Right)
{
public override string ToString()
{
return Left + ":" + Right;
}
}
class Program
{
static void Main()
{
int right = 7;
Pair pair = new Pair(5, right);
var (left, rightValue) = pair;
int total = left + rightValue;
Console.WriteLine($"right={right}");
Console.WriteLine($"total={total}");
}
}
using System;
record Pair(int Left, int Right)
{
public override string ToString()
{
return Left + ":" + Right;
}
}
class Program
{
static void Main()
{
int right = 3;
Pair pair = new Pair(5, right);
var (left, rightValue) = pair;
int total = left + rightValue;
Console.WriteLine($"right={right}");
Console.WriteLine($"total={total}");
}
}
using System;
record Pair(int Left, int Right)
{
public override string ToString()
{
return Left + ":" + Right;
}
}
class Program
{
static void Main()
{
int right = 10;
Pair pair = new Pair(5, right);
var (left, rightValue) = pair;
int total = left + rightValue;
Console.WriteLine($"right={right}");
Console.WriteLine($"total={total}");
}
}
right ← 7, pair ← 5:7, total ← 12
12{13 static void Main()14 {15 int right→ 7 = 7; //@right=3, 1016 Pair pair→ 5:7 = new Pair(5, right);17 var (left, rightValue) = pair5:7;18 int total→ 12 = left5 + rightValue7;1920 Console.WriteLine($"right={right7}");21 Console.WriteLine($"total={total12}");22 }outputright=7 total=12
right ← 3, pair ← 5:3, total ← 8
12{13 static void Main()14 {15 int right→ 3 = 3;16 Pair pair→ 5:3 = new Pair(5, right);17 var (left, rightValue) = pair5:3;18 int total→ 8 = left5 + rightValue3;1920 Console.WriteLine($"right={right3}");21 Console.WriteLine($"total={total8}");22 }outputright=3 total=8
right ← 10, pair ← 5:10, total ← 15
12{13 static void Main()14 {15 int right→ 10 = 10;16 Pair pair→ 5:10 = new Pair(5, right);17 var (left, rightValue) = pair5:10;18 int total→ 15 = left5 + rightValue10;1920 Console.WriteLine($"right={right10}");21 Console.WriteLine($"total={total15}");22 }outputright=10 total=15