[C #7] 1. Tuples (Tuples ),

Source: Internet
Author: User

[C #7] 1. Tuples (Tuples ),
1. Old Version code

1 class Program 2 {3 static void Main (string [] args) 4 {5 var fullName = GetFullName (); 6 7 Console. writeLine (fullName. item1); // Item1, 2, 3 can not bear, 8 Console. writeLine (fullName. item2); 9 Console. writeLine (fullName. item3); 10} 11 static Tuple <string, string, string> GetFullName () => new Tuple <string, string, string> ("first name", "blackheart ", "last name"); 12}

In some scenarios, we need a method to return more than one returned value. NET 4 introduces the Tuple generic class, which allows us to return multiple parameters. Each parameter is named Item1 in order; Item2, Item3, some of them solve our problem, but for obsessive-compulsive programmers, the names of Item1, 2, and 3 cannot be tolerated. so, in C #7, A new generic type ValueTuple <T> is introduced to solve this problem. This type is located in a separate dll (System. valueTuple), you can use nuget to introduce to your current project (https://www.nuget.org/packages/System.ValueTuple ).

2. ValueTuple

Look at the Code directly:

1 class Program 2 {3 static void Main (string [] args) 4 {5 var fullName = GetFullName (); 6 7 Console. writeLine (fullName. first); // finally it can not be Item1, 2, 3, 8 Console. writeLine (fullName. middle); 9 Console. writeLine (fullName. last); 10} 11 12 static (string First, string Middle, string Last) GetFullName () => ("first name", "blackheart", "last name "); 13}

Have you seen the difference? We can finally replace the damn "Item1, 2, 3" with a more intuitive name. It looks great. However, it seems that we have not used the System. ValueTuple mentioned above. Let's open the compiled assembly and look at it:

1 internal class Program 2 {3 private static void Main (string [] args) 4 {5 ValueTuple <string, string, string> fullName = Program. getFullName (); 6 Console. writeLine (fullName. item1); // you are still Item1, 2, 3, FUCK !!! 7. Console. writeLine (fullName. item2); 8 Console. writeLine (fullName. item3); 9} 10 11 [TupleElementNames (new string [] 12 {13 "First", 14 "Middle", 15 "Last" 16})] 17 private static ValueTuple <string, string, string> GetFullName () 18 {19 return new ValueTuple <string, string, string> ("first name", "blackheart ", "last name"); 20} 21}

I don't know. I was shocked. Our fullName. First was compiled and still fullName. Item1...

The difference lies in the GetFullName method. The compiler translates our simplified syntax into ValueTuple <string, string, string> and adds a new Attribute (TupleElementNamesAttribute ), then we store the User-Defined "First", "Middle", and "Last" as metadata. Like ValueTuple, TupleElementNamesAttribute is located in a separate dll of System. ValueTuple.

3. Summary

The new syntax form is indeed more intuitive and friendly. but, in essence, is still implemented by using generic types. At the same time, the compiler must support the new syntax form.

After learning about what the essence is, you can use it with confidence if it is allowed in the project environment (Where the ValueTuple type can appear, (first, last) The new syntax form can all be).

Reference: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

 

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.