Value Tuples Basics
Create lightweight tuples for grouped data.
Value Tuples Basics is a free C# Academy lesson on CoddyKit. This is lesson 1 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.
What Is a Tuple?
A tuple groups several values into one lightweight bundle without defining a class. C# value tuples use parentheses: (int, string).
using System;
class Program
{
static void Main()
{
(int, string) pair = (1, "one");
Console.WriteLine(pair.Item1 + " = " + pair.Item2);
}
}Creating a Value Tuple
Write the values in parentheses. The compiler infers the tuple type from them.
using System;
class Program
{
static void Main()
{
var point = (3, 4);
Console.WriteLine("x=" + point.Item1 + ", y=" + point.Item2);
}
}Accessing Elements: Item1, Item2
Unnamed tuple elements are reached through Item1, Item2, Item3, and so on, in order.
using System;
class Program
{
static void Main()
{
var rgb = (255, 128, 0);
Console.WriteLine("R=" + rgb.Item1 + " G=" + rgb.Item2 + " B=" + rgb.Item3);
}
}Tuples Hold Mixed Types
Each element can be a different type. A tuple can mix numbers, strings, booleans, and more.
using System;
class Program
{
static void Main()
{
var user = ("Ada", 36, true);
Console.WriteLine(user.Item1 + ", age " + user.Item2 + ", active=" + user.Item3);
}
}Tuples Are Mutable
Value tuple elements can be reassigned, since each ItemN is a writable field.
using System;
class Program
{
static void Main()
{
var score = (player: "A", points: 0);
score.points = 10;
score.points += 5;
Console.WriteLine(score.player + ": " + score.points);
}
}Tuples as Local Bundles
Tuples are great for temporarily grouping values inside a method without creating a dedicated type.
using System;
class Program
{
static void Main()
{
var range = (min: 5, max: 50);
int span = range.max - range.min;
Console.WriteLine("Span: " + span);
}
}Comparing Tuples
Value tuples support equality: two tuples are equal if all corresponding elements are equal.
using System;
class Program
{
static void Main()
{
var a = (1, 2);
var b = (1, 2);
var c = (1, 3);
Console.WriteLine(a == b);
Console.WriteLine(a == c);
}
}Nesting Tuples
A tuple element can itself be a tuple, letting you build small structured groups.
using System;
class Program
{
static void Main()
{
var line = (start: (0, 0), end: (3, 4));
Console.WriteLine("From " + line.start.Item1 + "," + line.start.Item2 +
" to " + line.end.Item1 + "," + line.end.Item2);
}
}Tuples in Collections
You can store tuples in a list to keep paired data together without a class.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var scores = new List<(string, int)>
{
("Ann", 90),
("Bob", 85)
};
foreach (var s in scores)
Console.WriteLine(s.Item1 + ": " + s.Item2);
}
}Value Semantics
A value tuple is a struct, so assigning it copies the values. Changing the copy does not affect the original.
using System;
class Program
{
static void Main()
{
var original = (n: 1);
var copy = original;
copy.n = 99;
Console.WriteLine("original=" + original.n + ", copy=" + copy.n);
}
}Putting It Together
Value tuples give you quick, typed groupings ideal for short-lived data inside a method or a simple list.
using System;
class Program
{
static void Main()
{
var result = (count: 3, total: 150);
double average = (double)result.total / result.count;
Console.WriteLine("Average: " + average.ToString("0.0"));
}
}Quick Check
Test your understanding of value tuples.
Recap
A value tuple bundles multiple values in parentheses like (int, string) without a class. Unnamed elements are accessed via Item1, Item2. Tuples are mutable structs with value semantics and support equality. They are ideal for quick, local groupings.
using System;
class Program
{
static void Main()
{
var t = (1, "one", true);
Console.WriteLine(t.Item1 + " " + t.Item2 + " " + t.Item3);
}
}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 “Value Tuples Basics” lesson free?
Yes — the full text of “Value Tuples Basics” 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 “Value Tuples Basics”?
Create lightweight tuples for grouped data. 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 1 of 4.
How long does the “Value Tuples Basics” 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