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 6-Tuple in C#?
A 6-tuple in C# is represented by the Tuple<T1,T2,T3,T4,T5,T6> class, which is a data structure that holds exactly six elements of potentially different types. Tuples are useful for grouping related data together without creating a custom class or struct.
The 6-tuple provides six properties to access its elements: Item1 through Item6, each corresponding to the respective component in the tuple.
Syntax
Following is the syntax for creating a 6-tuple −
Tuple<T1, T2, T3, T4, T5, T6> tupleName = new Tuple<T1, T2, T3, T4, T5, T6>(value1, value2, value3, value4, value5, value6);
You can also use the Tuple.Create() method for shorter syntax −
var tupleName = Tuple.Create(value1, value2, value3, value4, value5, value6);
Properties
The 6-tuple has six properties to access its components −
-
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
-
Item6 − Gets the value of the sixth component
Using Constructor to Create 6-Tuple
Here's an example demonstrating how to create a 6-tuple using the constructor and access its elements −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string, int, string, int, int, string> tuple =
new Tuple<string, int, string, int, int, string>("jack", 150, "pete", 300, 600, "allan");
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);
Console.WriteLine("Value (Item6)= " + tuple.Item6);
if (tuple.Item1 == "jack") {
Console.WriteLine("Found: Tuple Item 1 = " + tuple.Item1);
}
if (tuple.Item3 == "pete") {
Console.WriteLine("Found: Tuple Item 3 = " + tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Found: Tuple Item 4 = " + tuple.Item4);
}
if (tuple.Item6 == "allan") {
Console.WriteLine("Found: Tuple Item 6 = " + tuple.Item6);
}
}
}
The output of the above code is −
Value (Item1)= jack Value (Item2)= 150 Value (Item3)= pete Value (Item4)= 300 Value (Item5)= 600 Value (Item6)= allan Found: Tuple Item 1 = jack Found: Tuple Item 3 = pete Found: Tuple Item 4 = 300 Found: Tuple Item 6 = allan
Using Tuple.Create() Method
The Tuple.Create() method provides a more concise way to create tuples without explicitly specifying generic type parameters −
using System;
public class Demo {
public static void Main(string[] args) {
var tuple = Tuple.Create(100, 150, 200, 300, 600, 1000);
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);
Console.WriteLine("Value (Item6)= " + tuple.Item6);
Console.WriteLine("Sum of all items: " +
(tuple.Item1 + tuple.Item2 + tuple.Item3 + tuple.Item4 + tuple.Item5 + tuple.Item6));
}
}
The output of the above code is −
Value (Item1)= 100 Value (Item2)= 150 Value (Item3)= 200 Value (Item4)= 300 Value (Item5)= 600 Value (Item6)= 1000 Sum of all items: 2350
Practical Example with Mixed Data Types
Here's a practical example showing how a 6-tuple can store employee information −
using System;
public class Demo {
public static void Main(string[] args) {
var employee = Tuple.Create("John Doe", 101, "Software Engineer", 75000.50, true, "IT");
Console.WriteLine("Employee Details:");
Console.WriteLine("Name: " + employee.Item1);
Console.WriteLine("ID: " + employee.Item2);
Console.WriteLine("Position: " + employee.Item3);
Console.WriteLine("Salary: $" + employee.Item4);
Console.WriteLine("Is Active: " + employee.Item5);
Console.WriteLine("Department: " + employee.Item6);
if (employee.Item5) {
Console.WriteLine(employee.Item1 + " is currently active in " + employee.Item6 + " department.");
}
}
}
The output of the above code is −
Employee Details: Name: John Doe ID: 101 Position: Software Engineer Salary: $75000.5 Is Active: True Department: IT John Doe is currently active in IT department.
Conclusion
The 6-tuple in C# provides a convenient way to group six related values of different types without creating a custom class. You can create 6-tuples using either the constructor or the Tuple.Create() method, and access elements through the Item1 through Item6 properties.
