The C# tuples is a data structure that was introduced in the C#.net version 4.0. The tuple data structure is designed to hold elements that are of different data types. Tuples help in returning multiple values from a class method in a single parameter which has many advantages over Out Parameters, class or structs types, or dynamic return type. As the parameters are passed into a single data set, it becomes easy to access this data set and perform different operations on it.
Tuples can be created in two different ways
The constructor for creating the tuple is present in Tuple
Tuple <T1> (T1)
Example:
Tuple<int> Tuple_example = new Tuple<int>(27); Console.WriteLine(Tuple_example); Console.ReadLine();
Output:
Tuple <T1, T2> (T1, T2)
Example:
Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(1, "cat", true); Console.WriteLine(tuple.Item1); Console.WriteLine(tuple.Item2.ToString()); Console.ReadLine();
Output:
C# provides a static Create method to create tuple as follows
Create (T1);
Example:
var Tuple_example = Tuple.Create(27); Console.WriteLine(Tuple_example); Console.ReadLine();
Output:
Create (T1, T2);
Example:
var Tuple_example = Tuple.Create(1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
Output:
While using the constructor, we need to specify the data type of every element while creating the tuple. The Create methods helps us in eliminating the cumbersome coding as shown above.
The generic tuple is a reference type it means the values are stored on heap, which makes its usage costly in terms of memory and performance. C#7.0 introduced a new and improved version of Tuple over generic tuple and named it as ValueTuple. The ValueTuple is stored on the heap, which is easy to retrieve. The value tuple comes with .NET Framework 4.7 or .NET library 2.0. To separately install the tuple functionality, you need to install the NuGet Package called System.Value.Tuple.
Important points about ValueTuple
Example:
var Tuple_example = (1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
Output:
This is equivalent to:
var Tuple_example = Tuple.Create(1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
Example:
(int, string, bool) Tuple_example = (1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
Output:
Example:
details.Item1; – returns 28 details.Item2; -- returns ”CBC”
Example:
var detail = (28); --this is not a tuple var details = (28, “CBC”); -- this is a tuple
In the first statement, the compiler will not consider ‘detail’ as a tuple, instead, it will be considered and a normal ‘var’ type.
(int ID, String Firstname, string SecondName) details = (28, “CBC”, “C# Tuples”);
var nestedtuple_example = new Tuple <int, string, string, int, int, int, string, Tuple<double, int, string>> (5, “This”, “is”, 7,8,9, “number”, Tuple.Create (17.33, 29,”April”));
{ Var multiplication = tupleexample.Item1 * tupleexample.Item2; Console.WriteLine (“Multiplication is”, {0}, multiplication); }
The to the method TupleExampleMethod would look like
TupleExampleMethod(new Tuple<int, int> (34,56));
public static Tuple <int, string> GetPerson() { return Tuple.Create (1, “abc”); }
Let’s create a program in Visual to understand how tuple works.
The values from both textboxes are taken into a tuple and the tuple is passed on to a method.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnMultiply_Click(object sender, EventArgs e) { int value1 = Convert.ToInt32(txtVal1.Text); int value2 = Convert.ToInt32(TxtVal2.Text); CallMethod(new Tuple<int, int>(value1, value2)); } private void CallMethod(Tuple<int, int> tuple) { txtResult.Text = Convert.ToString(tuple.Item1 * tuple.Item2); Console.ReadLine(); } } }
The result is displayed in the third text box named as txtResult. End result looks like.
The tuple data structure is a reference type, which means the values are stored on the heap instead of stack. This makes usage of tuples and accessing them in the program an intensive CPU task. The only 8 elements in tuples property is one of the major drawbacks of tuples as nested tuples are more prone to induce ambiguity. Also accessing elements in tuple with Item
The above is the detailed content of C# Tuples. For more information, please follow other related articles on the PHP Chinese website!