C # tuple<t1,t2 .... t> Use of tuples

Source: Internet
Author: User

1)

First say the tuple: a data structure, separated by commas, that is used to pass a combination of values to a program or operating system.

NET Framework directly supports tuples of one to seven elements

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>
Rest property by using the objects in the nested tuple Tuple < T1, T2, T3, T4, T5, T6, T7, trest ; object.

A simple example:
            The tuple of an element tuple<int> test = new Tuple<int> (34);            Two elements of the tuple 1<n<8 tuple<string, int> test2 = tuple.create<string, int> ("str", 2);            Tuple<int, int> test2_1 = new Tuple<int, int> (2,2); Tuple of 8 elements (note,tuple< type ...);: Base "type" up to 7, eighth element type must also be a tuple) tuple<int, int, int, int, int, int, int, TUPLE&L t;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 also be tuple<int, int, tuple<int, int>> test_i_i_tii = new Tuple<int, int, tuple<int, int&gt                        ;> (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 value issues
In general, we use the Out keyword (compared to other languages, such as the Golang,out keyword or a little bit of a hassle), we can do this by using a tuple:
Namespace tupledemo{class Program {static void Main (string[] args) {//Use out to get multiple return values            String outparam = "";            int returnvalue = Funoutparamdemo (out outparam);            Console.WriteLine (returnvalue + "" + Outparam);            Use tuples to get multiple return values Tuple<int, string> r = Funtupleparamdemo ();            Console.WriteLine (R.item1 + "" + r.item2);        Console.read (); }///<summary>///OUT keyword, implementation returns two return values///</summary>//<param name= "O" ><            /param>//<returns></returns> public static int Funoutparamdemo (out string o) {            o = "ReturnValue";        return 10; }///<summary>//Use tuples to implement "indirect" return "two" return values///</summary>//<returns></ret urns> public static Tuple<int, string> Funtupleparamdemo () {return new tuple<int, String&gt ;(10,"ReturnValue"); }    }}
Operation Result:



C # tuple<t1,t2 .... t> Use of tuples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.