skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

Tuples and deconstruction

C# 7

Code card: Tuples and deconstruction. Program output: Min: 4 / Range: 4..42

Return a named tuple, then deconstruct it into variables. No class or out parameters. Need two values back from a method? It is available since C# 7. The key line is var (min, max) = MinMax(data) in tuples.cs. Run it and the output reads Min: 4, Range: 4..42.

Did this tip save you some time? Consider buying me a coffee to fuel the next one.

Buy me a coffee
tuples.cs
int[] data = [4, 8, 15, 16, 23, 42];
var stats = MinMax(data);
Console.WriteLine($"Min: {stats.Min}");
var (min, max) = MinMax(data);
Console.WriteLine($"Range: {min}..{max}");
static (int Min, int Max) MinMax(int[] xs) =>
(xs.Min(), xs.Max());
Output
$ dotnet run tuples.cs
Min: 4
Range: 4..42