Tuples and deconstruction
Tuples and deconstruction
A tuple groups a fixed number of values without declaring a new named domain type. C# tuple syntax such as (int Min, int Max) uses System.ValueTuple value types. Element names help readability but do not change the underlying tuple type identity. A deconstruction assigns individual components to separate variables. Use a tuple for small local results; choose a record or class when a concept deserves a stable named contract with validation or behavior.
System.Tuple is a separate reference-type family. Anonymous types created with new { ... } are compiler-generated reference types with read-only properties; they are useful for local projections, but do not provide a named public return type.
using System;var bounds = Bounds(new[] { 8, 2, 9, 2 });Console.WriteLine(bounds.Min);Console.WriteLine(bounds.Max);static (int Min, int Max) Bounds(int[] values){ if (values.Length == 0) throw new ArgumentException("Need values"); int min = values[0], max = values[0]; foreach (int value in values) { if (value < min) min = value; if (value > max) max = value; } return (min, max);}The function returns both bounds as one value. It explicitly rejects an empty array, avoiding an invented minimum or maximum.
Expected output (reasoned, not executed):
29Build on the first example
using System;var (name, count) = ("Tape", 5);Console.WriteLine(name);Console.WriteLine(count);var (_, remaining) = ("Box", 3);Console.WriteLine(remaining);var original = (Rows: 2, Columns: 4);var copy = original;copy.Columns = 6;Console.WriteLine(original.Columns);Deconstruction binds components by position. The underscore in this declaration discards the first value. The tuple assignment copies the value, so changing a numeric field on copy leaves original unchanged.
Expected output (reasoned, not executed):
Tape534Apply the idea to another case
using System;var first = (Width: 3, Height: 5);var second = (X: 3, Y: 5);Console.WriteLine(first == second);var (label, quantity) = new Stock("Cable", 7);Console.WriteLine(label);Console.WriteLine(quantity);record Stock(string Label, int Quantity);The two tuples have equal component values despite different names. The positional record supplies a generated Deconstruct method; its components are assigned in declaration order.
Expected output (reasoned, not executed):
TrueCable7Common mistakes and design choices
Tuple names are not runtime labels that enforce domain meaning. Swapping two same-typed components can compile and still be wrong. Prefer a named record when passing values across a long-lived API boundary. Tuple equality operators compare corresponding elements; member equality semantics still apply (for example, lists are not automatically compared element-by-element). Use an explicit empty-input policy for calculations.
Practice
Write a method Summary(int[] values) returning (int Count, int Total). Assume inputs are small nonnegative integers whose sum fits in int. Test [4, 6, 0] and an empty array, then deconstruct the result.
Use the manual practice task and knowledge check to verify your understanding. All examples target C# 14 / .NET 10; execution and ByteMind renderer support remain unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tuples and deconstruction — knowledge check
Apply the lesson concepts to the following scenarios.
What runtime type family implements C# tuple syntax?
Do element names determine tuple type identity?
What does var (_, count) = ("Box", 3) do?
What happens on an empty input in the Bounds example?
When is a named record preferable to a tuple?