C # Use of Tuple (T1, t2... T) tuples
1)
Group element: a data structure separated by commas (,). It is a combination of a series of values passed to a program or operating system.
NET Framework directly supports one to seven elements of tuples
Tuple Tuple Tuple Tuple Tuple Tuple Tuple
In addition, you can create eight or more elements of the Tuple In the Rest attribute through the objects in the nested tuples. Object.
Simple Example:
// The Tuple of an element
Test = new Tuple
(34); // The tuples of the two elements 1
Test2 = Tuple. Create
("Str", 2); Tuple
Test2_1 = new Tuple
(); // The tuples of 8 elements (Note: Tuple <type...>: a maximum of 7 basic "types", and the eighth element type must also be tuples) Tuple
> Test3 = new Tuple
> (1, 2, 3, 4, 5, 6, 7, new Tuple
(8); // Tuple
> Test_ I _ I _Tii = new Tuple
> (1, 1, new Tuple
(2, 3); Console. writeLine (test. item1); Console. writeLine (test2.Item1 + test2.Item2); Console. writeLine (test2_1.Item1 + test2_1.Item2); Console. writeLine (test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest. item1 );
Result:
2) Multiple return values
Generally, we use the out keyword (which is a little more troublesome than other languages such as golang and out keyword). At this time, we can use the tuples for implementation:
Namespace TupleDemo {class Program {static void Main (string [] args) {// use out to get multiple returned values string outparam = ""; int returnvalue = FunOutParamDemo (out outparam ); console. writeLine (returnvalue + "" + outparam); // use the tuples to get multiple returned values. Tuple
R = FunTupleParamDemo (); Console. WriteLine (r. Item1 + "" + r. Item2); Console. Read ();}///
/// Out keyword, which returns two return values //////
///
Public static int FunOutParamDemo (out string o) {o = "returnValue"; return 10 ;}///
/// Use tuples to achieve [indirect] return [two] return values //////
Public static Tuple
FunTupleParamDemo () {return new Tuple
(10, "returnValue ");}}}
Running result: