Named Tuple Elements
Give tuple fields meaningful names.
Naming Tuple Elements
Instead of Item1/Item2, you can give tuple elements names for readable code: (int Id, string Name).
using System;
class Program
{
static void Main()
{
(int Id, string Name) user = (1, "Ada");
Console.WriteLine(user.Id + ": " + user.Name);
}
}Names in the Literal
You can attach names directly when creating the tuple, and the compiler remembers them.
using System;
class Program
{
static void Main()
{
var point = (X: 3, Y: 4);
Console.WriteLine("X=" + point.X + ", Y=" + point.Y);
}
}All lessons in this course
- Value Tuples Basics
- Named Tuple Elements
- Deconstruction
- Tuples as Method Return Values