Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to declare a tuple in C#?
A tuple in C# is a data structure that can hold multiple values of different types. Tuples are useful when you need to return multiple values from a method or group related data together without creating a separate class.
C# provides two main ways to declare tuples: the classic Tuple<T1, T2> class and the newer value tuples introduced in C# 7.0 with cleaner syntax.
Syntax
Classic tuple syntax −
Tuple<int, string> tuple = new Tuple<int, string>(value1, value2);
Value tuple syntax (C# 7.0+) −
(int, string) tuple = (value1, value2); // or with named elements (int id, string name) tuple = (value1, value2);
Using Classic Tuple Declaration
The traditional way uses the Tuple<T1, T2> class where you access elements using Item1, Item2, etc. −
using System;
class Program {
static void Main(string[] args) {
Tuple<int, string> tuple = new Tuple<int, string>(50, "Tom");
Console.WriteLine("First item: " + tuple.Item1);
Console.WriteLine("Second item: " + tuple.Item2);
if (tuple.Item1 == 50) {
Console.WriteLine("ID matches: " + tuple.Item1);
}
if (tuple.Item2 == "Tom") {
Console.WriteLine("Name matches: " + tuple.Item2);
}
}
}
The output of the above code is −
First item: 50 Second item: Tom ID matches: 50 Name matches: Tom
Using Value Tuples (C# 7.0+)
Value tuples provide a more readable syntax and better performance. You can use unnamed or named elements −
using System;
class Program {
static void Main(string[] args) {
// Unnamed value tuple
(int, string) tuple1 = (25, "Alice");
Console.WriteLine($"Unnamed: {tuple1.Item1}, {tuple1.Item2}");
// Named value tuple
(int id, string name) tuple2 = (30, "Bob");
Console.WriteLine($"Named: {tuple2.id}, {tuple2.name}");
// Alternative declaration
var tuple3 = (age: 35, city: "New York");
Console.WriteLine($"Alternative: {tuple3.age}, {tuple3.city}");
}
}
The output of the above code is −
Unnamed: 25, Alice Named: 30, Bob Alternative: 35, New York
Tuples with Multiple Data Types
Tuples can hold more than two values and mix different data types −
using System;
class Program {
static void Main(string[] args) {
// Tuple with three different types
(int id, string name, bool isActive) employee = (101, "John", true);
Console.WriteLine($"Employee ID: {employee.id}");
Console.WriteLine($"Employee Name: {employee.name}");
Console.WriteLine($"Is Active: {employee.isActive}");
// Tuple with four elements
var product = (id: 1001, name: "Laptop", price: 999.99, inStock: true);
Console.WriteLine($"Product: {product.name}, Price: ${product.price}");
}
}
The output of the above code is −
Employee ID: 101 Employee Name: John Is Active: True Product: Laptop, Price: $999.99
Comparison: Classic Tuple vs Value Tuple
| Feature | Classic Tuple | Value Tuple |
|---|---|---|
| Syntax | Tuple<int, string> |
(int, string) |
| Element Access | Item1, Item2 |
Item1 or named elements |
| Performance | Reference type (heap) | Value type (stack) |
| Readability | Less readable | More readable with names |
Conclusion
Tuples in C# provide an easy way to group multiple values without creating custom classes. While classic tuples use Tuple<T1, T2> syntax, value tuples offer better performance and readability with named elements. Use value tuples for modern C# development as they are more efficient and developer-friendly.
