C # Programming Guide (4) Tuple)

Source: Internet
Author: User

Group Meta is a new feature introduced by C #4.0. It must be Based on. NET Framework 4.0 or later. Group elements use generics to simplify the definition of a class.

 

The following code is used as an example:

 

 

  1     public class Point
2 {
3 public int X { get; set; }
4 public int Y { get; set; }
5 }
6
7
8 //the user customer data type.
9   Point p = new Point() { X = 10, Y = 20 };
10 //use the predefine generic tuple type.
11   Tuple<int, int> p2 = new Tuple<int, int>(10, 20);
12
13 //
14   Console.WriteLine(p.X + p.Y);
15 Console.WriteLine(p2.Item1 + p2.Item2);

 

 

A simple class that contains two Int type members. The traditional method definition point requires a lot of code, but there is only one sentence to use tuple. The group elements are mostly used for the return value of the method. If a function returns multiple types, you can define a tuple type instead of using output parameters such as out and ref. Very convenient.

 

The following columns are slightly more complex:

 

  1             //1 member
2   Tuple<int> test = new Tuple<int>(1);
3 //2 member ( 1< n <8 )
4   Tuple<int, int> test2 = Tuple.Create<int, int>(1,2);
5 //8 member , the last member must be tuple type.
6   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));
7
8 //
9   Console.WriteLine(test.Item1);
10 Console.WriteLine(test2.Item1 + test2.Item2);
11 Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);

 

The first definition contains a member.

The second definition contains two members and is initialized using the create method.

The third definition shows that tuple supports a maximum of 8 members. If there are more than 8 members, nesting is required. Note that 8th members are special. If there are 8 members, 8th must be nested to define tuple. As shown above.

 

 

Here are two examples of nested definitions:

 

 1             //2 member ,the second type is the nest type tuple.
2   Tuple<int, Tuple<int>> test4 = new Tuple<int, Tuple<int>>(1, new Tuple<int>(2));
3 //10 member datatype. nest the 8 parameter type.
4   Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> test5 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int, int, int>(8, 9, 10));
5
6
7 //
8   Console.WriteLine(test4.Item1 + test4.Item2.Item1);
9 Console.WriteLine(test5.Item1 + test5.Item2 + test5.Item3 + test5.Item4 + test5.Item5 + test5.Item6 + test5.Item7 + test5.Rest.Item1 + test5.Rest.Item2 + test5.Rest.Item3);

 

 

 

Sample Code: Download

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.