A tuple in C# is a lightweight data structure that groups multiple, potentially heterogeneous, data elements into a single composite value. Modern C# primarily utilizesDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
System.ValueTuple, a mutable value type introduced in C# 7.0, which provides concise language-level syntax for grouping variables without the overhead of defining a custom class or struct.
System.ValueTuple vs. System.Tuple
C# contains two distinct tuple implementations:
System.ValueTuple(Modern C# 7.0+): Astruct(value type) that is allocated inline. It is allocated on the stack when used as a local variable, but on the heap when embedded as a field within aclassor as an element in an array. It is mutable, exposes its elements as public fields, and supports language-level syntax for named elements.System.Tuple(Legacy C# 4.0): Aclass(reference type) allocated on the heap. It is immutable, exposes its elements as read-only properties, and requires verbose instantiation viaTuple.Create().
System.ValueTuple.
Syntax and Initialization
The C# compiler provides built-in syntax for declaring and initializingValueTuple instances using parentheses.
Method Signatures
Tuples can be declared as parameter types or return types in method signatures, utilizing the same parenthetical syntax.Deconstruction
Tuple deconstruction extracts the elements of a tuple into distinct local variables in a single operation. This is handled by the compiler mapping the tuple fields to the declared variables based on their ordinal position.Underlying Mechanics and Metadata
The named elements in aValueTuple are syntactic sugar. At the Common Language Runtime (CLR) level, a ValueTuple only possesses Item1, Item2, Item3, etc., fields.
During the current compilation, the C# compiler tracks custom element names using its internal syntax tree and symbol table. For local variables, these names are entirely erased at runtime. However, when tuples are used in members (such as fields, properties, or method signatures), the compiler emits a TupleElementNamesAttribute into the Intermediate Language (IL) metadata. This attribute persists the names across assembly boundaries, allowing consuming code to read the custom names from the compiled assembly.
Because names are not part of the underlying CLR type, tuples with identical arity and types but different element names are structurally compatible and can be assigned to one another.
Tuple Equality
Starting with C# 7.3, tuples support the== and != operators. The compiler evaluates tuple equality by performing a pairwise, short-circuiting comparison of each element in ordinal order.
== operator must be defined between the two corresponding types of each element pair.
Hashing and Composite Keys
A critical architectural benefit ofSystem.ValueTuple is its built-in, value-based implementation of IEquatable<T> and GetHashCode(). The GetHashCode() method computes a combined hash code from all underlying elements.
This deterministic, value-based hashing makes tuples ideal for use as composite keys in hash-based collections like Dictionary<TKey, TValue> or HashSet<T>, eliminating the need to define custom structures solely for key grouping.
Master C# with Deep Grasping Methodology!Learn More





