Tuple is a new data structure type added after C# 7.1 that
can be used to represent a composite data type (a bit like the composition pattern in design patterns)
. The first time I knew the word tuple was in Python, and now in C# It is also convenient to use this data type in
Java, just like java also supports the var keyword. Each programming language is gradually moving closer, absorbing the advantages of each other.
We should also use the code flexibly to exert the power of the code.
Use of region Tuple and ValueTuple
//Create a Tuple (it can be found that Tuple can hold 1-n data Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(10, "hello", true); //Get the components of Tuple (each item will be encapsulated as an attribute of Item (only get method), so it can only be assigned at initialization int a = tuple.Item1; string b = tuple.Item2; bool c = tuple.Item3; //You can create an array of Tuples (and can be nested Tuple<int, string>[] tuples = new Tuple<int, string>[] { new Tuple<int, string>(100, "s"), new Tuple<int, string>(1000, "sss") };
//ValueTuple is an enhanced version of Tuple, with an empty structure, and each item can be assigned a value ValueTuple<int, string, bool> vaa = new ValueTuple<int, string, bool>(); vaa.Item1 = 10; vaa.Item2 = "Hello World"; vaa.Item3 = true; //Tuple and ValueTuple are also useful when returning multiple values, see the following example for details
method returns multiple values
//ValueTuple is an enhanced version of Tuple, with an empty structure, and each item can be assigned a value ValueTuple<int, string, bool> vaa = new ValueTuple<int, string, bool>(); vaa.Item1 = 10; vaa.Item2 = "Hello World"; vaa.Item3 = true; //Tuple and ValueTuple are also useful when returning multiple values, see the following example for details
//1. Use the out parameter to return multiple values //---------The variables of out parameters need to be declared externally, and there is no unified management for out parameters int bb = 0, cc = 0; int aa = GetY(1, out bb,out cc); Console.WriteLine("aa={0} bb={1} cc={2}",aa,bb,cc); //2. Use Tuple to return multiple values //------------- There is no array out of bounds, no forced conversion, no external variables, but the name of the Item cannot be customized var va = GetZ (); int data_aa = va.Item1; string data_bb = va.Item2; bool data_cc = va.Item3; Console.WriteLine("data_aa={0} data_bb={1} data_cc={2}", data_aa, data_bb, data_cc); //3. Use ValueTuple to return multiple values //--------No external variables are required, there is no array out of bounds, data can be accessed in a custom way instead of Item, no forced conversion is required //----- encapsulating it into an object is the best choice var vall = Get(1, "Ai"); Console.WriteLine("id={0}\tname={1}\tpass={2}",vall.id,vall.name,vall.isPass);return multiple values_method declaration
//array returns multiple values static object[] GetX() { return new object[] { 1, "Hi", true }; } //out returns multiple values static int GetY(int cc,out int a,out int b) { a = 10; b = 20; return cc; } //Tuple returns multiple values static Tuple<int,string,bool> GetZ() { return new Tuple<int, string, bool>(10, "hi_tuple", false); } //valueTuple returns multiple values static (int id,string name,bool isPass) Get(int a, string b) { return (a,b,true); }