How to use named tuples in C#
By FoxLearn 3/5/2025 3:51:58 AM 24
However, the default tuple field names, such as Item1, Item2, etc., aren't very descriptive. Instead, you can give these fields meaningful names to make your code clearer and easier to understand.
For example, How to create and use a named tuple:
var bookTuple = (title: "1984", author: "George Orwell"); Console.WriteLine(bookTuple.title); // outputs 1984 Console.WriteLine(bookTuple.author); // outputs George Orwell
Using named fields makes it easier to work with tuples. bookTuple.title
is more intuitive than bookTuple.Item1
. While the underlying tuple is still a ValueTuple<string, string>
, the field names you assign title
and author
make the code more readable and self-explanatory.
1. Create a Named Tuple
There are several ways to create and assign values to a named tuple:
Option 1: Declare and Assign at the Same Time
The field types are inferred based on the assigned values:
var book = (title: "1984", author: "George Orwell");
Option 2: Declare Fields and Types, Then Assign Values
Here you first declare the tuple with specific types and later assign values:
(string title, string author) book; book.title = "1984"; book.author = "George Orwell";
Option 3: Declare Fields and Types, Then Use Deconstruction Assignment
You can declare the fields and types and assign values all at once using deconstruction:
(string title, string author) book; book = ("1984", "George Orwell");
2. Returning a Named Tuple from a Method
You can return a named tuple from a method as a way to return multiple values more clearly than using out
parameters. Here's an example:
(string title, string author) GetBook() { return (title: "Brave New World", author: "Aldous Huxley"); }
To access the fields from the returned tuple:
var book = GetBook(); Console.WriteLine(book.title); // outputs Brave New World
3. Using a Named Tuple as a Method Parameter
You can also pass a named tuple as a parameter to a method.
void PrintBookDetails((string title, string author) book) { Console.WriteLine($"Book Title: {book.title}, Author: {book.author}"); }
To call the method, you pass in the named tuple:
PrintBookDetails((title: "To Kill a Mockingbird", author: "Harper Lee")); // Outputs: Book Title: To Kill a Mockingbird, Author: Harper Lee
Alternatively, you don’t have to use the named field names in the calling code. As long as the tuple field types match, the method will work:
PrintBookDetails(("To Kill a Mockingbird", "Harper Lee")); // Outputs: Book Title: To Kill a Mockingbird, Author: Harper Lee
Named tuples provide clarity by giving descriptive names to tuple fields. They can be created and used in several ways, returned from methods, or passed as parameters to methods, making your C# code cleaner and more readable.
- How to Parser a CSV file in C#
- How to read configuration from appsettings.json in C#
- How to Programmatically Update appsettings.json in C#
- How to deconstruct an object in C#
- Handling CSV Files Without a Header Row Using CsvHelper
- CSVHelper: Header with name not found
- How to Convert a Comma-Separated String into a List of Integers in C#
- How to send synchronous requests with HttpClient in C#