0Pricing
C# Academy · Lesson

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

  1. Value Tuples Basics
  2. Named Tuple Elements
  3. Deconstruction
  4. Tuples as Method Return Values
← Back to C# Academy