Modern C# · Lesson 5

Tuples and deconstruction

Return two related results without an output parameter.

How it works

A tuple groups a small number of related values into one result. Named elements improve readability at the return boundary, while deconstruction gives each element a local name at the call site. Bounds walks the supplied array through Min and Max and returns both extremes. The tuple itself does not explain what an empty input should mean; those sequence operations would throw for an empty integer array.

Decide that behavior explicitly when turning this example into a reusable method. Use a named record instead when the data travels through many methods, needs validation, or represents a concept with its own behavior.

Change the order of values and confirm the bounds stay the same. Then decide whether an empty input should fail, return null, or use a different result type.

A working example

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

var (minimum, maximum) = Bounds(new[] { 8, 3, 11 });
Console.WriteLine($"{minimum}:{maximum}");
(int Min, int Max) Bounds(int[] values) => (values.Min(), values.Max());

Expected output

3:11
Try this
Tuple names document positions; the positions still determine deconstruction.