How many things do you remember about Tuple C #4.0 new features ?,

Source: Internet
Author: User

How many things do you remember about Tuple C #4.0 new features ?,

The content shared today is based on a message received a few days ago. The message is as follows:

 

After reading the comments from this netizen, I believe that many new children's shoes are also confused. What are the best methods besides using new classes as bridges? Many children's shoes will surely think of DataSet, which is good. DataSet can fully implement the transmission of various datasets and is very convenient. However, we generally recommend using List sets, how can a List set pass a set containing multiple fields when attributes and classes are not determined? Creating a class for some simple structures is obviously unreasonable.

I. Tuple in 4.0 New Features

You can review a new feature added in c #4.0, Tuple, which is a generic set of fixed members:


Let's take a look at an official example:

Create a Tuple array containing seven elements

// Create a 7-tuple.

Var population = new Tuple <string, int, int> ("New York", 7891957,778 1984, 7894862,707 1639, 7322564,800 8278 ); // Display the first and last elements.

Console. WriteLine ("Population of {0} in 2000: {1: N0}", population. Item1, population. Item7 );

The usage is very simple and convenient. We may need to use the following methods:

Public class {

Public int ID {get; set ;}

Public string Name {get; set ;}

}

A a = new A () {ID = 1001, Name = 'codel '};

Console. WriteLine (a. Name );


To use Tuple, we only need:

Tuple <int, string> a = new Tuple <int, string> (1001, 'codel ');

Console. WriteLine (a. Item2); // Item1 indicates the first and Item2 indicates the second.


In this way, we can create a class for some simple structures or objects.
Note that tuple supports a maximum of 8 members. Note that 8th members are special. If there are 8 members, 8th must be nested tuple types.

Columns such as: Tuple <string, int, Tuple <int, int, int> the red part is 8th.

Usage of 8th elements: object. Rest. Item1,Object. Rest. Item2

 

Ii. Examples of actual scenarios

We use Tuple to transfer a simple dataset.

Use Tuple as the return value

Public List <Tuple <int, string, DateTime> GetUsers (){

String SQL = "select ID, Name, CreateTime from Tbl_Users ";

Using (SqlDataReader r = SqlHelper. ExecuteReader (Connections. AdminDBConString, CommandType. Text, SQL ))

{

List <Tuple <int, string, DateTime> list = new List <Tuple <int, string, DateTime> ();

While (r. Read ())

{

Tuple <int, string, DateTime> t = new Tuple <int, string, DateTime> (Convert. toInt32 (r [0]), r [1]. toString (), Convert. toDatetime (r [2]);

List. Add (t );

}

Return list;

}

}

Ease of use:

List <Tuple <int, string, datetime> list = GetUsers ();

Foreach (Tuple <int, string, datetime> t in list)

{

Console. write (t. Item1); // ID

Console. write (t. Item2); // Name

Console. write (t. Item3); // CreateTime

}

Now, do you understand that the above issue could have been solved in this way.

I personally recommend that you use this in some simple structures. The complex object structure is subject to some limitations of the Tuple itself (for example, the Tuple can support up to 8 items and the TRest must be used if the content exceeds the limit) however, the operation is complex. In actual development, Tuple can be nested with a maximum of Tuple layers.

You can learn more about common basic knowledge in your daily work and study. Many seemingly inconspicuous things will play a very important role in the development process.

You can leave a message on the public account to obtain related resources or ask other questions.If you have excellent original technical articles, you can contribute articles and share them with others to earn a reward!

Scan to get more development resources:

 

Related Article

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.