C#tuples (meta-group)

Source: Internet
Author: User

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 endure,,, 8         Console.WriteLine (FULLNAME.ITEM2); 9         Console.WriteLine ( FULLNAME.ITEM3);     }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 return value, Microsoft introduced a tuple in. NET 4 generic class, you can allow us to return multiple parameters, each parameter is named Item1;item2,item3 in order, Part of the solution to our problem, but for obsessive-compulsive programmers, item1,2,3 's name is simply not tolerated,, so, in C#7, introduced a new generic type valuetuple<t> to solve this problem, This type is in a separate DLL (System.valuetuple) that can be introduced into your current project (http://www.php.cn/) through NuGet.

2. ValueTuple

No nonsense, look directly at the code:


1 class Program 2 {3     static void Main (string[] args) 4     {5         var fullName = Getfullname (); 6  7         Console. WriteLine (Fullname.first);  Finally can not item1,2,3,,, 8         Console.WriteLine (fullname.middle); 9         Console.WriteLine (fullname.last);     One     of the static (string first, string middle, String last) getfullname () = ("First name", "Blackheart", "Last Name"); 13}

Do you see the difference? We can finally replace the damn "item1,2,3" with a more intuitive name, and it looks great. But it seems that we didn't use the system.valuetuple I mentioned above, we turned the compiled assembly to see:


1 internal class Program 2 {3     private static void Main (string[] args) 4     {5         valuetuple<string, String, str ing> fullName = Program.getfullname (); 6         Console.WriteLine (FULLNAME.ITEM1);//original you still Item1,2,3,,,fuck!!! 7         Console.WriteLine (FULLNAME.ITEM2); 8         Console.WriteLine (FULLNAME.ITEM3); 9     }     [Tupleelementnames (New string[]12 {"First             ",             "Middle", "last             "     )]17     private static Valuetuple<string, String, string> getfullname ()     {         return new valuetuple<string, String, string> ("First name", "Blackheart", "Last Name");     }21}

Do not see do not know, a look startled, original our Fullname.first , after compiling incredibly still fullname.item1 , really day dog ...

The difference is that getfullname this method, the compiler translates our simplified syntax form into valuetuple<string, string, string > added a new attribute (Tupleelementnamesattribute) and then saved our custom very intuitive "first", "Middle" and "last" as metadata ( If it is only partially used, no such metadata will be added . Tupleelementnamesattribute As with Valuetuple, it is located in a separate DLL in System.valuetuple.

3. Example


 1 class Program 2 {3 static void Main (string[] args) 4 {5 var range = (first:1, end:10); 6// Can also write, the effect is the same, after compiling are no traces of first,end,, first and end is only the grammatical level of the camouflage 7//(int first, int last) range = (1, 10); 8 Console.WriteLine (Range.first); 9 Console.WriteLine (Range.End); 10 11//You can use Var, which does not show the way to declare a variable will compile redundant code, with caution, do not know whether it is not yet optimized. (var begin, var end) = (DateTime.Parse ("2017-1-1"), DateTime.Parse ("2017-12-31")), Console.WriteLine ( Begin); Console.WriteLine (end);//begin,end can be overwritten by renaming StartDate and EndDate, but there will be a compile warning indicating that the name is ignored.  //warning cs8123:the tuple element name ' begin ' is ignored because a different name was specified by the target  Type ' (DateTime startdate, datetime endDate) '//warning cs8123:the tuple element name ' end ' is ignored because A different name is specified by the target type ' (DateTime startdate, DateTime endDate) ' (DateTime startdate, DateTime endDate) Timespan = (begin:DateTime.Parse ("2017-1-1"), End:DateTime.Parse ("2017-12-31")); Console.WriteLine (timespan.startdate ); Console.WriteLine (timespan.enddate); 22}23}

Look at the compiled code:


1 private static void Main (string[] args) 2 {3     valuetuple<int, int> range = new Valuetuple<int, int> (1, 1 0); 4     Console.WriteLine (range. ITEM1); 5     Console.WriteLine (range. ITEM2); 6     valuetuple<datetime, datetime> expr_3c = new Valuetuple<datetime, datetime> (DateTime.Parse (" 2017-1-1 "), DateTime.Parse (" 2017-12-31 ")); 7     DateTime item = expr_3c. Item1; 8     DateTime item2 = expr_3c. ITEM2; 9     datetime begin = item;10     datetime end = Item2;11     Console.WriteLine (begin);     Console.WriteLine (End );     valuetuple<datetime, datetime> timeSpan = new Valuetuple<datetime, datetime> (DateTime.Parse (" 2017-1-1 "), DateTime.Parse (" 2017-12-31 ")),     Console.WriteLine (timespan.item1),     Console.WriteLine ( TIMESPAN.ITEM2); 16}

Note (var begin, var end) = (DateTime.Parse ("2017-1-1"), DateTime.Parse ("2017-12-31")); The cheap result of this line looks pretty bad (the 6-10 lines above), probably the problem of insufficient compilation optimizations (the same is true for release compilation).

4. Summary

The new grammatical form is really intuitive and friendly, but the essence is still implemented with generic types, and the compiler is required to support the new syntax form.

After understanding what the essence is, later in the project environment permitting, it is safe to use it boldly ( type valuetuple can appear, (first,last) This new grammatical form can be ).

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.