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 3-Tuple or Triple Tuple in C#?
The Tuple<T1, T2, T3> class represents a 3-tuple, also called a triple. A tuple is a data structure that contains a sequence of elements of different types, providing a convenient way to group related data together without creating a custom class.
3-tuples are commonly used for −
-
Easier access to a data set with three related values.
-
Easier manipulation of grouped data.
-
To represent a single set of three related values.
-
To return multiple values from a method.
-
To pass multiple values to a method as a single parameter.
Syntax
Following is the syntax for creating a 3-tuple −
Tuple<T1, T2, T3> tuple = new Tuple<T1, T2, T3>(item1, item2, item3);
Accessing tuple elements −
tuple.Item1 // First element tuple.Item2 // Second element tuple.Item3 // Third element
Using Mixed Data Types
A 3-tuple can store different data types in each position −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int, string, string> tuple = new Tuple<int, string, string>(35, "steve", "katie");
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
if (tuple.Item1 == 35) {
Console.WriteLine("Exists: Tuple Value = " + tuple.Item1);
}
if (tuple.Item2 == "jack") {
Console.WriteLine("Exists: Tuple Value = " + tuple.Item2);
}
if (tuple.Item3 == "katie") {
Console.WriteLine("Exists: Tuple Value = " + tuple.Item3);
}
}
}
The output of the above code is −
Value (Item1)= 35 Value (Item2)= steve Value (Item3)= katie Exists: Tuple Value = 35 Exists: Tuple Value = katie
Using Same Data Types
All three elements in a tuple can also be of the same type −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string, string, string> tuple = new Tuple<string, string, string>("nathan", "steve", "katie");
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
if (tuple.Item1 == "nathan") {
Console.WriteLine("Exists: Tuple Value = " + tuple.Item1);
}
if (tuple.Item2 == "jack") {
Console.WriteLine("Exists: Tuple Value = " + tuple.Item2);
}
if (tuple.Item3 == "katie") {
Console.WriteLine("Exists: Tuple Value = " + tuple.Item3);
}
}
}
The output of the above code is −
Value (Item1)= nathan Value (Item2)= steve Value (Item3)= katie Exists: Tuple Value = nathan Exists: Tuple Value = katie
Using Tuple.Create Method
You can also create a 3-tuple using the Tuple.Create method, which infers the types automatically −
using System;
public class Demo {
public static void Main(string[] args) {
var coordinates = Tuple.Create(10.5, 20.3, 15.7);
Console.WriteLine("X coordinate: " + coordinates.Item1);
Console.WriteLine("Y coordinate: " + coordinates.Item2);
Console.WriteLine("Z coordinate: " + coordinates.Item3);
Console.WriteLine("Type: " + coordinates.GetType());
}
}
The output of the above code is −
X coordinate: 10.5 Y coordinate: 20.3 Z coordinate: 15.7 Type: System.Tuple`3[System.Double,System.Double,System.Double]
Conclusion
The 3-tuple in C# provides a simple way to group three related values of any type without creating a custom class. You can create tuples using the constructor or the Tuple.Create method, and access elements using Item1, Item2, and Item3 properties.
