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 create 2-tuple or pair tuple in C#?
The Tuple<T1, T2> class in C# represents a 2-tuple, also known as a pair. A tuple is a data structure that contains a sequence of elements of different types. The 2-tuple holds exactly two values that can be accessed using Item1 and Item2 properties.
2-tuples are useful when you need to return two values from a method or group two related values together without creating a separate class.
Syntax
Following is the syntax for creating a 2-tuple −
Tuple<T1, T2> tuple = new Tuple<T1, T2>(value1, value2);
Accessing tuple elements −
var firstValue = tuple.Item1; var secondValue = tuple.Item2;
Using String Pair Tuple
This example demonstrates creating a 2-tuple with two string values −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string, string> tuple = new Tuple<string, string>("jack", "steve");
Console.WriteLine("First Value = " + tuple.Item1);
Console.WriteLine("Second Value = " + tuple.Item2);
if (tuple.Item1 == "jack") {
Console.WriteLine("Found: First tuple value = " + tuple.Item1);
}
if (tuple.Item2 == "steve") {
Console.WriteLine("Found: Second tuple value = " + tuple.Item2);
}
}
}
The output of the above code is −
First Value = jack Second Value = steve Found: First tuple value = jack Found: Second tuple value = steve
Using Mixed Type Tuple
This example shows a 2-tuple with different data types (integer and string) −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int, string> tuple = new Tuple<int, string>(20, "steve");
Console.WriteLine("Integer Value = " + tuple.Item1);
Console.WriteLine("String Value = " + tuple.Item2);
if (tuple.Item1 == 20) {
Console.WriteLine("Found: Integer value = " + tuple.Item1);
}
if (tuple.Item2 == "steve") {
Console.WriteLine("Found: String value = " + tuple.Item2);
}
}
}
The output of the above code is −
Integer Value = 20 String Value = steve Found: Integer value = 20 Found: String value = steve
Using Tuple.Create Method
C# provides a more convenient way to create tuples using the Tuple.Create method −
using System;
public class Demo {
public static void Main(string[] args) {
var studentInfo = Tuple.Create("Alice", 85);
var coordinates = Tuple.Create(10.5, 20.3);
Console.WriteLine("Student: " + studentInfo.Item1 + ", Score: " + studentInfo.Item2);
Console.WriteLine("X: " + coordinates.Item1 + ", Y: " + coordinates.Item2);
// Tuple comparison
var point1 = Tuple.Create(5, 10);
var point2 = Tuple.Create(5, 10);
Console.WriteLine("Points are equal: " + point1.Equals(point2));
}
}
The output of the above code is −
Student: Alice, Score: 85 X: 10.5, Y: 20.3 Points are equal: True
Conclusion
2-tuples in C# provide a simple way to group two related values of different types without creating a custom class. They are particularly useful for returning multiple values from methods or temporarily pairing data. Use Tuple.Create for more concise syntax and cleaner code.
