Working with Tuples in C#: Grouping Multiple Values into a Single Unit
Learn about tuples in C#, lightweight data structures that group multiple values of different types. This tutorial explains how to create tuples, access their elements, use tuples for returning multiple values from methods, and leverage their benefits for cleaner and more efficient C# code.
Working with Tuples in C#
Introduction
A tuple in C# is a lightweight data structure that groups multiple values into a single unit. These values can be of different data types. Tuples are useful for representing a set of related data, returning multiple values from a method, and passing multiple values as a single parameter.
Creating Tuples
You can create tuples in several ways. The `Tuple` class provides static methods (`Create`) to create tuples of various sizes (1-tuple, 2-tuple, 3-tuple, and so on, up to 8-tuples). Alternatively, you can create tuples using a more concise syntax (introduced in C# 7).
Tuple Class Methods
Method Name | Description |
---|---|
Tuple.Create(T1) |
Creates a 1-tuple (singleton). |
Tuple.Create(T1, T2) |
Creates a 2-tuple (pair). |
Tuple.Create(T1, T2, T3) |
Creates a 3-tuple (triple). |
Tuple.Create(T1, T2, T3, T4) |
Creates a 4-tuple (quadruple). |
Tuple.Create(T1, T2, T3, T4, T5) |
Creates a 5-tuple (quintuple). |
Tuple.Create(T1, T2, T3, T4, T5, T6) |
Creates a 6-tuple (sextuple). |
Tuple.Create(T1, T2, T3, T4, T5, T6, T7) |
Creates a 7-tuple (septuple). |
Tuple.Create(T1, T2, T3, T4, T5, T6, T7, T8) |
Creates an 8-tuple (octuple). |
Example 1: Creating a Tuple (Without Static Method)
Example 1: Tuple Creation
using System;
namespace CSharpFeatures {
class TupleExample {
public static void Main(string[] args) {
var book = new Tuple<string, string, double>("C# in Depth", "Jon Skeet", 100.50);
Console.WriteLine("-----------------Book's Record---------------------");
Console.WriteLine($"Title: {book.Item1}");
Console.WriteLine($"Author: {book.Item2}");
Console.WriteLine($"Price: {book.Item3}");
}
}
}
Example 1 Output
-----------------Book's Record---------------------
Title: C# in Depth
Author: Jon Skeet
Price: 100.5
Example 2: Creating a Tuple (Using Static Method)
Example 2: Tuple Creation with Static Method
using System;
namespace CSharpFeatures {
class TupleExample {
public static void Main(string[] args) {
var book = Tuple.Create("C# in Depth", "Jon Skeet", 100.50);
Console.WriteLine("-----------------Book's Record---------------------");
Console.WriteLine($"Title: {book.Item1}");
Console.WriteLine($"Author: {book.Item2}");
Console.WriteLine($"Price: {book.Item3}");
}
}
}
Example 3: Returning Multiple Values from a Method
Example 3: Returning Multiple Values
using System;
namespace CSharpFeatures {
public class Student {
public static void Main(string[] args) {
var (name, email) = Show();
Console.WriteLine($"{name} {email}"); // Output: irfan irfan@gmail.com
}
static (string name, string email) Show() {
return ("irfan", "irfan@gmail.com");
}
}
}
Conclusion
Tuples offer a flexible and concise way to group related data in C#. Their ability to return multiple values from methods and to be passed as single parameters simplifies code and enhances readability.