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)