Named Tuple Elements
Give tuple fields meaningful names.
Named Tuple Elements is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
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);
}
}Why Names Improve Clarity
Named elements document intent. result.Total is far clearer than result.Item2.
using System;
class Program
{
static void Main()
{
var result = (Count: 3, Total: 150);
Console.WriteLine("Avg: " + ((double)result.Total / result.Count).ToString("0.0"));
}
}Names Are Optional Labels
Tuple element names are a compile-time convenience. Underneath, the fields are still Item1, Item2; both names work.
using System;
class Program
{
static void Main()
{
var p = (Age: 30, City: "Rome");
Console.WriteLine(p.Age + " / " + p.Item1); // same value
Console.WriteLine(p.City + " / " + p.Item2);
}
}Named Tuples in Method Signatures
A method can declare a named tuple as its parameter or return type, making the API self-describing.
using System;
class Program
{
static (int Sum, int Product) Compute(int a, int b)
{
return (a + b, a * b);
}
static void Main()
{
var r = Compute(3, 4);
Console.WriteLine("Sum=" + r.Sum + ", Product=" + r.Product);
}
}Mixing Named and Positional
You can name some elements and leave others positional, though naming all of them is clearer.
using System;
class Program
{
static void Main()
{
var data = (Id: 7, "guest", Active: true);
Console.WriteLine(data.Id + ", " + data.Item2 + ", " + data.Active);
}
}Names with Inferred Members
When you build a tuple from named variables, C# can infer matching element names automatically.
using System;
class Program
{
static void Main()
{
int width = 10, height = 5;
var size = (width, height); // names inferred from variables
Console.WriteLine("w=" + size.width + ", h=" + size.height);
}
}Updating Named Elements
Named elements are still writable fields, so you can reassign them as the program runs.
using System;
class Program
{
static void Main()
{
var game = (Player: "A", Score: 0);
game.Score += 25;
Console.WriteLine(game.Player + " scored " + game.Score);
}
}Named Tuples in Lists
Lists of named tuples read almost like rows in a table.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var people = new List<(string Name, int Age)>
{
("Ann", 30),
("Bob", 25)
};
foreach (var p in people)
Console.WriteLine(p.Name + " is " + p.Age);
}
}Equality with Names
Names do not affect equality. Two tuples are equal when their values match, regardless of element names.
using System;
class Program
{
static void Main()
{
var a = (X: 1, Y: 2);
var b = (A: 1, B: 2);
Console.WriteLine(a == b); // True - values match
}
}Putting It Together
Named tuples combine the lightness of tuples with the readability of named fields, perfect for clear method returns.
using System;
class Program
{
static (bool Ok, string Message) Validate(int age)
{
if (age < 0) return (false, "Age cannot be negative");
return (true, "Valid");
}
static void Main()
{
var v = Validate(-1);
Console.WriteLine(v.Ok + ": " + v.Message);
}
}Quick Check
Test your understanding of named tuple elements.
Recap
Named tuple elements like (int Id, string Name) make tuples self-documenting; you access them by name instead of Item1. Names can be declared, inferred from variables, and used in method signatures. They are compile-time labels and do not affect equality, which compares values.
using System;
class Program
{
static void Main()
{
var user = (Id: 42, Name: "Kai");
Console.WriteLine(user.Name + " #" + user.Id);
}
}Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Named Tuple Elements” lesson free?
Yes — the full text of “Named Tuple Elements” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Named Tuple Elements”?
Give tuple fields meaningful names. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Named Tuple Elements” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Value Tuples Basics
- Named Tuple Elements
- Deconstruction
- Tuples as Method Return Values