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 7-Tuple or Septuple in C#?
The Tuple<T1,T2,T3,T4,T5,T6,T7> class represents a 7-tuple, also known as a septuple. A tuple is a data structure that contains a fixed number of elements in a specific sequence, allowing you to group related values together.
7-tuples are commonly used for −
- Easier access to a data set with seven related values.
- Easier manipulation of a data set.
- To represent a single set of data with seven components.
- To return multiple values from a method.
- To pass multiple values to a method.
Syntax
Following is the syntax for creating a 7-tuple in C# −
Tuple<T1, T2, T3, T4, T5, T6, T7> tuple = new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
You can also use the Tuple.Create() method for shorter syntax −
var tuple = Tuple.Create(item1, item2, item3, item4, item5, item6, item7);
Using Constructor to Create 7-Tuple
Example
Let us see an example to implement the 7-tuple using the constructor −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int, int, int, int, int, int, int> tuple = new Tuple<int, int, int, int, int, int, int>(100, 150, 200, 300, 600, 1000, 2000);
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("Value (Item7)= " + tuple.Item7);
if (tuple.Item5 == 600) {
Console.WriteLine("Exists: Tuple Item 5 = " + tuple.Item5);
}
if (tuple.Item6 == 900) {
Console.WriteLine("Exists: Tuple Item 6 = " + tuple.Item6);
}
if (tuple.Item7 == 2000) {
Console.WriteLine("Exists: Tuple Item 7 = " + tuple.Item7);
}
}
}
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 Value (Item7)= 2000 Exists: Tuple Item 5 = 600 Exists: Tuple Item 7 = 2000
Using Tuple.Create() Method
Example
You can also use the Tuple.Create() method for a more concise syntax −
using System;
public class Demo {
public static void Main(string[] args) {
var tuple = Tuple.Create("John", "Doe", 25, "Engineer", "Microsoft", 75000.50, true);
Console.WriteLine("First Name: " + tuple.Item1);
Console.WriteLine("Last Name: " + tuple.Item2);
Console.WriteLine("Age: " + tuple.Item3);
Console.WriteLine("Job Title: " + tuple.Item4);
Console.WriteLine("Company: " + tuple.Item5);
Console.WriteLine("Salary: " + tuple.Item6);
Console.WriteLine("Is Active: " + tuple.Item7);
}
}
The output of the above code is −
First Name: John Last Name: Doe Age: 25 Job Title: Engineer Company: Microsoft Salary: 75000.5 Is Active: True
Comparing Tuple Items
Example
Let us see another example that demonstrates comparing tuple items −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int, int, int, int, int, int, int> tuple = new Tuple<int, int, int, int, int, int, int>(100, 150, 200, 300, 600, 1000, 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("Value (Item7)= " + tuple.Item7);
if (tuple.Item5 == 600) {
Console.WriteLine("Exists: Tuple Item 5 = " + tuple.Item5);
}
if (tuple.Item6 == 900) {
Console.WriteLine("Exists: Tuple Item 6 = " + tuple.Item6);
}
if (tuple.Item7 == 2000) {
Console.WriteLine("Exists: Tuple Item 7 = " + tuple.Item7);
}
if (tuple.Item7 == tuple.Item6) {
Console.WriteLine("Tuple Items Matched!");
}
}
}
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 Value (Item7)= 1000 Exists: Tuple Item 5 = 600 Tuple Items Matched!
Conclusion
The 7-tuple in C# allows you to group seven related values of different types into a single data structure. You can create 7-tuples using either the constructor syntax or the more convenient Tuple.Create() method, and access individual elements using the Item1 through Item7 properties.
