Brief Introduction to the tuple type in. net4.0

Source: Internet
Author: User
Technorati labels:. net4.0, tuple, new features

Microsoft described tuple in msdn as "a tuple is a data structure that has a specific number and sequence of values. due to the limited level of E, I understand that tuple is a data structure with a specified number and order. For this data structure,. net4.0 provides a set of tuple types, as follows:

Tuple  
Tuple <t>  
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>  
Tuple <T1, T2, T3, T4, T5, T6, T7, trest>  

We can see from the declaration of these tuple types. net4.0 provides a tuple from one value to a maximum of eight values. We can create a generic tuple instance using the static method of the first tuple, for example, tuple <string, int> T = tuple. create ("key", 789); you can also use the generic tuple constructor to create an instance, such as tuple <string, int> T = new tuple <string, int> ("key", 789); for a generic tuple, we can use its attributes Item1, item2 ...... Item7 and rest are used to access its contained values.Note that the itemx and rest attributes of the tuple type are read-only.

When will we use tuple after we know the definition of the tuple type? For tuple <t>, I think most of the time we don't use such a data type to wrap it up and call it.(Some people mentioned that tuple is used to interact with languages such as F #. I am not very familiar with the details about F #, maybe it can be used when interacting with F)Aside from tuple <t>, other tuple <> types bring great convenience for program development. In the past, when we had to process a number of related data, such as employee ID, employee age, and employee start time, we defined a new employee class to make the programming structure reasonable, let it contain attributes such as ID, age, and employment time (of course, you can declare three variables respectively). Now, we only need to declare a tuple <string, Int, datetime> object, eliminating the need to define a new class step, of course, we do this at the cost of code readability, after all, when we access a tuple object, we access its members through properties similar to item without actual meanings.

In msdn, several aspects of using the tuple type are provided:

  • It is used to represent a group of independent data, such as a record in the database. This is a good understanding. every item in tuple is like every field in a record.
  • We can declare a tuple array to provide datasets that are easy to access and operate;
  • When we want the function to return multiple values, we can use the tuple type as the return value of the function, so that we can define multiple parameters without using ref;
  • When we want to pass multiple values through one parameter, we can pass a tuple type to the function parameter. For example, thread is called during multi-threaded programming. in the past, if multiple values need to be passed during start (object), we can only declare a New Type. Now we can use tuple.

After reading the introduction to the tuple type in msdn, I raised some questions and thought about these questions. The following are my questions and my own opinions, for your reference, please kindly advise if there is any inaccuracy!

  • For the 2-tuple [] (tuple <T1, T2> []) array, what is the difference between it and our commonly used dictionary <tkey, tvalue>? On the surface, they can all represent a set of two data types. My personal opinion is that dictionary provides more functions to access or process the data stored by it, which can be seen from its many Members and methods, therefore, when we only need to transfer or save data, using 2-tuple will produce a smaller system overhead.
  • What is the difference between tuple <> type and VAR? I think there is no difference in the use of a method. But don't forget that the scope of the anonymous type is limited to the method body, And tuple <> as a practical type, it is more widely used than anonymous methods.
  • What if I want to define more than 8 tuple members? Are you expecting Microsoft to open tuple for more members? No! The simplest way is to declare the last member of tuple as tuple <T1, T2, T3, T4, T5, T6, T7, tuple <t1, t2……>. However, I personally think that if you need more than n members, you should define an actual class, so that the code is much more readable. It is hard to imagine that you can remember what a dozen items represent, and other people will crash when reading such code !!

The above is my understanding of the tuple type. If you have any questions, please kindly advise !!

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.