Records and Modern Types
Deconstruction
Records can be deconstructed into separate variables.
Deconstruction
Deconstruction.cs
using System;
record Pair(int Left, int Right)
{
public override string ToString()
{
return Left + ":" + Right;
}
}
class Program
{
static void Main()
{
int right = ;
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 = ;
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 = ;
Pair pair = new Pair(5, right);
var (left, rightValue) = pair;
int total = left + rightValue;
Console.WriteLine($"right={right}");
Console.WriteLine($"total={total}");
}
}
deconstruction
Deconstruction splits a value into named local variables.