Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Named tuple elements are compile-time constructs in C# that assign semantic identifiers to the fields of a System.ValueTuple structure. They replace the default Item1, Item2, …, ItemN field names with user-defined aliases, providing stronger typing semantics at the source-code level without altering the underlying runtime type.

Syntax and Initialization

Named elements can be defined either on the left-hand side (type declaration) or the right-hand side (value initialization) of an assignment.
// Explicitly typed declaration (Left-hand side)
(int Id, string Username) user = (1, "admin");

// Implicitly typed declaration with named values (Right-hand side)
var configuration = (Port: 8080, Protocol: "HTTPS");

// Mixed declaration (Left-hand side names take precedence)
(int Code, string Message) response = (Id: 404, Text: "Not Found");
Starting with C# 7.1, the compiler supports tuple projection initializers, which automatically infer the element names from the variables or properties used to initialize the tuple.
int count = 10;
string status = "Pending";

// The compiler infers the names 'count' and 'status'
var operation = (count, status); 

Console.WriteLine(operation.count); // Outputs: 10

Compiler Mechanics and Metadata

Named tuples are syntactic sugar. At the Common Language Runtime (CLR) level, named tuples do not exist; they are compiled down to standard System.ValueTuple<T1, T2, ...> structs. The compiler handles the translation of your custom names to the underlying ItemN fields. Because of this, the default ItemN fields remain accessible even when custom names are provided.
var point = (X: 15, Y: 30);

// Both access the exact same underlying memory location
Console.WriteLine(point.X);     // Outputs: 15
Console.WriteLine(point.Item1); // Outputs: 15
When a named tuple is returned from a method or exposed as a property, the compiler preserves the element names across assembly boundaries by applying the [TupleElementNames] attribute to the member in the emitted Intermediate Language (IL) metadata.

Assignment and Type Compatibility

Tuple element names are not part of the tuple’s strict type identity. Type compatibility during assignment is determined exclusively by the arity (number of elements) and the sequence of types. Element names are entirely ignored during assignment.
(int Alpha, int Beta) tupleA = (10, 20);
(int Gamma, int Delta) tupleB = (50, 60);

// Valid assignment: Arity (2) and types (int, int) match exactly.
tupleA = tupleB;

// tupleA.Alpha is now 50. The name 'Gamma' is dropped.

Equality and Comparison

Because element names are erased at runtime, the equality operators (== and !=) evaluate tuples based on positional values, bypassing the names completely.
var source = (A: 1, B: 2);
var target = (X: 1, Y: 2);

// Evaluates to true. The compiler compares Item1 to Item1, and Item2 to Item2.
bool isMatch = source == target; 

Deconstruction

When deconstructing a named tuple into separate variables, the extraction is strictly positional. The names of the tuple elements do not need to match the names of the target variables.
var dimensions = (Width: 1920, Height: 1080);

// Positional deconstruction: 'Width' goes to 'x', 'Height' goes to 'y'
(int x, int y) = dimensions;
Master C# with Deep Grasping Methodology!Learn More