CSharp - Tuple Elements Naming

Introduction

You can give meaningful names to elements when creating tuple literals:

var tuple = (Name:"C#", Age:23);

Console.WriteLine (tuple.Name);     // C#
Console.WriteLine (tuple.Age);      // 23

You can do the same when specifying tuple types as return type from a method:

static (string Name, int Age) GetPerson() => ("C#", 23);

static void Main()
{
       var person = GetPerson();
       Console.WriteLine (person.Name);    // C#
       Console.WriteLine (person.Age);     // 23
}

After given field names you can still treat the elements as unnamed and refer to them as Item1, Item2, etc.

Tuples are type-compatible with one another if their element types match up in order.

Their element names need not to be the same.


(string Name, int Age, char Sex)  myTuple1 = ("C#", 23, 'M');
(string Age,  int Sex, char Name) myTuple2 = myTuple1;   // No error!