Tuples in. Net 4 (system. tuple)

Source: Internet
Author: User
Document directory
  •  
. Net 4 brings with it the tuple type! I have never worked with tuples in programming before and have a hard time seeing their purpose but here is an example of a tuple in C #:

 

What is tuple? In Chinese, we translate it into tuples. The concept of tuple is derived from the mathematical concept, indicating an ordered data set. In. net, tuple is implemented as a generic type. N-tuple indicates a tuple with n elements. The element of the set can be of any type.

 

VaR tupleone = new tuple <int, string> (1, "Hello World ");
Console. writeline ("tuple contains:" + tupleone. Item1 + "and" + tupleone. item2 );

There are also some factory methods for creating tuples:

VaR tupletwo = tuple. Create ("Hello World", 2010 );
VaR tuplethree = tuple. Create ("Hello World", 2010, new someclass ());
Console. writeline ("tuple contains:" + tuplethree. Item1 + "," + tuplethree. item2 + "and" + tuplethree. item3.myproperty );

Tuple facts:

  • Tuples are immutable
  • Tuples are reference types
  • Located in system. tuple
  • Can take up to 8 generic parameters, but can contain an infinite number of elements.
  • Elements are always named Item1, item2... Item7, rest

See msdn for more information.

Why tuple? This is a question worth weighing. In the above myrequest type, 3-tuple is used to encapsulate the required request information. Of course, we can also create a new struct for encapsulation. Both methods are competent. However, in actual programming practices, we often need a flexible data structure creation type. In many cases, the new data structure acts as a "temporary" role, there is no need to use the new type of dashboard, and tuple is designed for this experience. For example:

  • Point {x, y} indicates the data structure of the coordinate position.
  • Date {year, month, day} can represent the date structure; time {hour, minute, second} can represent the time structure; while datetime {date, time} can implement a flexible Date and Time Structure.
  • Request {name, URL, result}, which indicates several information about the request.
  • ..., As needed.

Http://technet.microsoft.com/zh-cn/library/dd387150 (V = vs.95)

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.