Value Tuples Basics
Create lightweight tuples for grouped data.
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);
}
}All lessons in this course
- Value Tuples Basics
- Named Tuple Elements
- Deconstruction
- Tuples as Method Return Values