Tuple
使用场景
-
To represent multiple data into a single data set.
-
To create, manipulate, and access data set.
-
To return multiple values from a method without using out parameters.
-
To pass multiple values to a method with the help of single parameters.
构造方法
- Constructor
// Constructor for single elements
Tuple (T1)
// Constructor for two elements
Tuple <T1, T2>(T1, T2)
// Constructor for eight elements
Tuple <T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
using System;
public class GFG{
// Main method
static public void Main (){
// Tuple with one element
Tuple<string>My_Tuple1 = new Tuple<string>("GeeksforGeeks");
// Tuple with three elements
Tuple<string, string, int>My_Tuple2 = new Tuple<string, string, int>("Romil", "Python", 29);
// Tuple with eight elements
Tuple<int, int, int, int, int, int, int, Tuple<int>>My_Tuple3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1,2,3,4,5,6,7, new Tuple<int>(8));
}
}
- Create
// Method for 1-tuple
Create(T1)
// Method for 2-tuple
Create(T1, T2)
// Method for 8-tuple
Create(T1, T2, T3, T4, T5, T6, T7, T8)
using System;
public class GFG {
// Main method
static public void Main()
{
// Creating 1-tuple
// Using Create Method
var My_Tuple1 = Tuple.Create("GeeksforGeeks");
// Creating 4-tuple
// Using Create Method
var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
// Creating 8-tuple
// Using Create Method
var My_Tuple3 = Tuple.Create(13, "Geeks", 67,
89.90, 'g', 39939, "geek", 10);
}
}
Accessing a Tuple
using System;
public class GFG {
// Main method
static public void Main()
{
// Creating 1-tuple
// Using Create Method
var My_Tuple1 = Tuple.Create("GeeksforGeeks");
// Accessing the element of Tuple
// Using Item property
Console.WriteLine("Element of My_Tuple1: " + My_Tuple1.Item1);
Console.WriteLine();
// Creating 4-tuple
// Using Create Method
var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
// Accessing the element of Tuple
// Using Item property
Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item1);
Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item2);
Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item3);
Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item4);
Console.WriteLine();
// Creating 8-tuple
// Using Create Method
var My_Tuple3 = Tuple.Create(13, "Geeks",
67, 89.90, 'g', 39939, "geek", 10);
// Accessing the element of Tuple
// Using Item property
// And print the 8th element of tuple
// using Rest property
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item1);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item2);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item3);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item4);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item5);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item6);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item7);
Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Rest);
}
}
Nested Tuples
// C# program to illustrate nested tuple
using System;
public class GFG{
// Main method
static public void Main ()
{
// Nested Tuple
var My_Tuple = Tuple.Create(13, "Geeks", 67, 89.90,
'g', 39939, "geek", Tuple.Create(12, 30, 40, 50));
// Accessing the element of Tuple
// Using Item property
// And accessing the elements of nested tuple
// Using Rest property
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item1);
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item2);
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item3);
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item4);
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item5);
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item6);
Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item7);
Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest);
Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item1);
Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item2);
Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item3);
Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item4);
}
}
Limitation of Tuple
- It is of reference type not of value type.
- It is limited to eight elements. Means you cannot store more than eight elements without nested tuple.
- These are only accessed by using Item property.