[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); 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); 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); 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. It turns out that our Console. WriteLine (fullName. First); after compilation, we actually started fullName. Item1. It's really a day...
The difference lies in the GetFullName method. The compiler translates our simplified syntax into ValueTuple <string, string, string> and adds a new Attribute (System. runtime. compilerServices. tupleElementNamesAttribute), and then save 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.
Reference: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/