Tuple: a data structure, separated by commas, for transmitting a series of program or operating system combination is worth
NET Framework directly supports one to seven elements have an array
Tuple<T1>
Tuple<T1,T2>
Tuple<T1,T2,T3>
Tuple<T1,T2,T3,T4>
Tuple<T1,T2,T3,T4,T5>
Tuple<T1,T2,T3,T4,T5,T6>
Tuple<T1,T2,T3,T4,T5,T6,T7>
Can also create eight / have a plurality of elements and then Rest tuple attributes Tuple <T1, T2, T3, T4, T5, T6, T7, TRestle> be nested objects by tuple objects obtained
Examples of single-:
- // elements of a tuple
- Tuple<int> test = new Tuple<int>(34);
- // tuples 1 <n two elements <8
- Tuple<string, int> test2 = Tuple.Create<string, int>("str", 2);
- Tuple<int, int> test2_1 = new Tuple<int, int>(2,2);
- // tuple element 8 (note, Tuple <... Type>: Basic "type" up to seven, the eighth element type must tuple)
- Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 =
- new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));
- // this can be
- Tuple<int, int, Tuple<int, int>> test_i_i_Tii = new Tuple<int, int, Tuple<int, int>>(1,1,new Tuple<int,int>(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);
Results:
2) multiple return values issues
in general we are using the out keyword (compared to other languages, such as golang, out keywords or a little bit of trouble), then we can use tuples to achieve:
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- // get use out multiple return values
- string outparam = "";
- int returnvalue = FunOutParamDemo(out outparam);
- Console.WriteLine(returnvalue + " " + outparam);
- // get the plurality of tuple values return
- Tuple<int, string> r = FunTupleParamDemo();
- Console.WriteLine(r.Item1 + " " + r.Item2);
- Console.Read();
- }
- /// <summary>
- /// out keywords, implementation returns two return values
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static int FunOutParamDemo(out string o)
- {
- o = "returnValue";
- return 10;
- }
- /// <summary>
- /// tuple achieve indirect] [[two] Return Value Returns
- /// </summary>
- /// <returns></returns>
- public static Tuple<int, string> FunTupleParamDemo() {
- return new Tuple<int, string>(10, "returnValue");
- }
- }
- }
operation result:
Original blog address https://www.cnblogs.com/mschen/p/8333903.html