Roadmap › C# Language — Basics to Advanced › Module: Arrays and Collections
Lessons in this course (38)

14. Tuple and ValueTuple

IntermediateMediumLesson 14 of 38
csharp
// System.Tuple (.NET 4): class, read-only, members named Item1..Item7
Tuple<int, string> t1 = Tuple.Create(1, "Asha");
Console.WriteLine(t1.Item2);

// System.ValueTuple (C# 7): struct, mutable fields, can have names, nice syntax
(int Id, string Name) t2 = (1, "Asha");
Console.WriteLine(t2.Name);
t2.Name = "Ravi";

(int Min, int Max) MinMax(int[] xs) => (xs.Min(), xs.Max());   // return several values
var (min, max) = MinMax([4, 9, 1]);                             // deconstruction
Console.WriteLine($"{min}-{max}");

(a, b) = (b, a);                      // swap without a temp variable
var counts = new Dictionary<(string City, int Year), int> { [("Pune", 2026)] = 10 };   // tuple as a composite key
Tuple<...>ValueTuple ( , )
KindClass (heap)Struct (no allocation)
MembersItem1, Item2 … read-only propertiesNamed fields, mutable
EqualityValue equality via Equals== and Equals compare values
UseLegacy APIsModern code: multiple return values, quick grouping

For public APIs with a meaning, prefer a record (record MinMax(int Min, int Max)) — tuple names are not part of the type at runtime.