Skip to main content

How to Use a Tuple with Named Elements in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

(int Quotient, int Remainder) is a tuple type with two named elements, so a method can return both numbers at once and the caller reads them as result.Quotient and result.Remainder. No class or record needed.

Microsoft's reference: The tuples feature provides a concise syntax to group multiple data elements in a lightweight data structure. The C# guide on the names: When you don't provide names, elements use default names Item1, Item2, and so on. Named elements make your code self-documenting without requiring a separate type definition.

Syntax​

Syntax
(<Type1> <Name1>, <Type2> <Name2>) <variable> = (<value1>, <value2>);

var <variable> = (<Name1>: <value1>, <Name2>: <value2>);

<variable>.<Name1>
PartWhat it is
(<Type1> <Name1>, <Type2> <Name2>)A tuple type with named elements. It goes wherever a type goes: a variable, a parameter or a method's return type
(<Name1>: <value1>, <Name2>: <value2>)A tuple value that names its own elements
<variable>.<Name1>Reads one element by its name

The reference names both places: You explicitly specify tuple field names in a tuple initialization expression or in the definition of a tuple type.

The Example​

A console app called NamedTuple has a Divide method that returns two numbers at once, the quotient and the remainder, as a tuple with named elements. It also builds a second tuple, a size, with the names written into the value.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n NamedTuple
cd NamedTuple
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/NamedTuple/NamedTuple.csproj:
Determining projects to restore...
Restored /Users/you/source/NamedTuple/NamedTuple.csproj (in 43 ms).
Restore succeeded.

dotnet new console -n NamedTuple creates a folder named NamedTuple with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or NamedTuple.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the code that reads the elements:

Program.cs (reading the elements)
var result = Divide(17, 5);

Console.WriteLine($"Quotient: {result.Quotient}");
Console.WriteLine($"Remainder: {result.Remainder}");
Console.WriteLine($"Whole tuple: {result}");
  • result.Quotient and result.Remainder read the two elements by name. var takes the type, names included, from the return type of Divide.
  • {result} prints the whole tuple.

Then a tuple that names its elements in the value:

Program.cs (names in the value)
// Names can also go right into the value.
var size = (Width: 4, Height: 3);
Console.WriteLine($"Area: {size.Width * size.Height}");

(Width: 4, Height: 3) creates a tuple and names both elements, so size.Width and size.Height work.

And the method, at the end of the file:

Program.cs (the method)
// The return type gives the two elements their names.
static (int Quotient, int Remainder) Divide(int dividend, int divisor)
{
return (dividend / divisor, dividend % divisor);
}

(int Quotient, int Remainder) is the return type: a tuple of two int elements, each with a name. The return statement builds the tuple without names, and the caller still sees them, because they come from the return type.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Quotient: 3
Remainder: 2
Whole tuple: (3, 2)
Area: 12

macOS-style terminal window titled Terminal — NamedTuple showing dotnet run printing Quotient: 3, Remainder: 2, Whole tuple: (3, 2) and Area: 12

17 divided by 5 is 3 with 2 left over, and each number came out under its own name. Printed whole, the tuple shows only its values, (3, 2). The size's two named elements multiplied to an area of 12.

The Complete File​

macOS-style code window titled Program.cs — NamedTuple showing a call to Divide with 17 and 5, three lines that print result.Quotient, result.Remainder and the whole tuple, a size tuple with the names Width and Height and a line that prints the area, and the Divide method whose return type is a tuple of int Quotient and int Remainder

Program.cs
var result = Divide(17, 5);

Console.WriteLine($"Quotient: {result.Quotient}");
Console.WriteLine($"Remainder: {result.Remainder}");
Console.WriteLine($"Whole tuple: {result}");

// Names can also go right into the value.
var size = (Width: 4, Height: 3);
Console.WriteLine($"Area: {size.Width * size.Height}");

// The return type gives the two elements their names.
static (int Quotient, int Remainder) Divide(int dividend, int divisor)
{
return (dividend / divisor, dividend % divisor);
}

Common Mistakes​

MistakeWhat happensDo this instead
Storing the result in a tuple type without names, as in (int, int) plain = Divide(17, 5);plain.Quotient stopped the build: error CS1061: '(int, int)' does not contain a definition for 'Quotient'Keep the names: use var, or write (int Quotient, int Remainder)
Listing the names in a different order, as in (int Remainder, int Quotient) swapped = Divide(17, 5);No error, but the values swapped: swapped.Quotient printed 2 and swapped.Remainder printed 3Write the names in the same order as the method's. The reference: The assignment process ignores the names of tuple fields
Giving the returned tuple other names, as in return (Answer: dividend / divisor, Leftover: dividend % divisor);Two warnings, starting with warning CS8123: The tuple element name 'Answer' is ignored because a different name or no name is specified by the target type '(int Quotient, int Remainder)'.Leave the names out of the returned value, or use the return type's names
Naming an element Rest, as in (Answer: 3, Rest: 2)error CS8126: Tuple element name 'Rest' is disallowed at any position.Choose another name. Microsoft's error reference: Names like Rest, ToString, GetHashCode, and Equals are reserved for ValueTuple infrastructure and can't be used as element names.

FAQ​

Can I still use Item1 and Item2?​

Yes. result.Item1 printed 3 and result.Item2 printed 2. The reference: You can always use the default name of a field, even when a field name is specified explicitly or inferred.

Can I change an element?​

Yes. size.Width = 10; worked, and the tuple printed (10, 3). The reference: tuple elements are public fields. That design makes tuples mutable value types.

Are the names there when the program runs?​

No. JsonSerializer.Serialize(result) wrote {}, and with IncludeFields = true it wrote {"Item1":3,"Item2":2}: the JSON knows only the default names. The reference: explicitly specified or inferred field names aren't available at run time. For data you send out, the reference suggests a named type: In public APIs, consider defining a class or a structure type.

How do I put the elements into separate variables?​

Take the tuple apart: var (quotient, remainder) = Divide(17, 5); printed 3 remainder 2.

Conclusion​

Name a tuple's elements in its type, (int Quotient, int Remainder), or in the value, (Width: 4, Height: 3), and read them as result.Quotient. The names help only while you write code: assignments go by position, and the running program knows only Item1, Item2 and so on.

Additional Resources​