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 5-Tuple or quintuple in C#?
A 5-tuple or quintuple in C# is represented by the Tuple<T1, T2, T3, T4, T5> class, which is a data structure that holds exactly five elements of potentially different types. Each element can be accessed through its corresponding Item property.
Syntax
Following is the syntax for creating a 5-tuple using the constructor −
Tuple<T1, T2, T3, T4, T5> tuple = new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
You can also use the Tuple.Create() method for shorter syntax −
var tuple = Tuple.Create(item1, item2, item3, item4, item5);
Properties
A 5-tuple has five read-only properties to access its elements −
-
Item1 − Gets the value of the first component.
-
Item2 − Gets the value of the second component.
-
Item3 − Gets the value of the third component.
-
Item4 − Gets the value of the fourth component.
-
Item5 − Gets the value of the fifth component.
Using Constructor to Create 5-Tuple
Example
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int, int, int, int, int> tuple = new Tuple<int, int, int, int, int>(120, 150, 270, 300, 600);
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
Console.WriteLine("Value (Item4)= " + tuple.Item4);
Console.WriteLine("Value (Item5)= " + tuple.Item5);
if (tuple.Item1 == 120) {
Console.WriteLine("Exists: Tuple Item 1 = " + tuple.Item1);
}
if (tuple.Item3 == 270) {
Console.WriteLine("Exists: Tuple Item 3 = " + tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Exists: Tuple Item 4 = " + tuple.Item4);
}
}
}
The output of the above code is −
Value (Item1)= 120 Value (Item2)= 150 Value (Item3)= 270 Value (Item4)= 300 Value (Item5)= 600 Exists: Tuple Item 1 = 120 Exists: Tuple Item 3 = 270 Exists: Tuple Item 4 = 300
Using Mixed Data Types
Example
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string, int, string, int, int> tuple = new Tuple<string, int, string, int, int>("jack", 150, "pete", 300, 600);
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
Console.WriteLine("Value (Item4)= " + tuple.Item4);
Console.WriteLine("Value (Item5)= " + tuple.Item5);
if (tuple.Item1 == "jack") {
Console.WriteLine("Exists: Tuple Item 1 = " + tuple.Item1);
}
if (tuple.Item3 == "pete") {
Console.WriteLine("Exists: Tuple Item 3 = " + tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Exists: Tuple Item 4 = " + tuple.Item4);
}
}
}
The output of the above code is −
Value (Item1)= jack Value (Item2)= 150 Value (Item3)= pete Value (Item4)= 300 Value (Item5)= 600 Exists: Tuple Item 1 = jack Exists: Tuple Item 3 = pete Exists: Tuple Item 4 = 300
Using Tuple.Create() Method
Example
using System;
public class Demo {
public static void Main(string[] args) {
var studentData = Tuple.Create("Alice", 21, "Computer Science", 3.8, 2025);
Console.WriteLine("Student Name: " + studentData.Item1);
Console.WriteLine("Age: " + studentData.Item2);
Console.WriteLine("Major: " + studentData.Item3);
Console.WriteLine("GPA: " + studentData.Item4);
Console.WriteLine("Graduation Year: " + studentData.Item5);
Console.WriteLine("\nComplete Student Record:");
Console.WriteLine($"{studentData.Item1}, {studentData.Item2} years old, {studentData.Item3} major, GPA: {studentData.Item4}, Graduating: {studentData.Item5}");
}
}
The output of the above code is −
Student Name: Alice Age: 21 Major: Computer Science GPA: 3.8 Graduation Year: 2025 Complete Student Record: Alice, 21 years old, Computer Science major, GPA: 3.8, Graduating: 2025
Conclusion
The 5-tuple in C# provides a convenient way to group five related values of different types into a single object. You can create it using either the constructor or Tuple.Create() method, and access elements through Item1 through Item5 properties.
