C #7.0 new feature 1: Based on Tuple's "multiple" return value method,
This article is based on the Issue in the Roslyn project: #347.
Review
First, let's raise a question: in C #, how can we make a method return "multiple" return values?
Let's review the practices of C #6.0 and earlier.
In C #, we usually have the following four methods to make a method return multiple data records.
- Use KeyValue combination
- 1 static void Main (string [] args) 2 {3 int int1 = 15; 4 int int2 = 25; 5 var result = Add_Multiply (int1, int2); 6 Console. writeLine (result. key); 7 Console. writeLine (result. value); 8} 9 10 private static KeyValuePair <int, int> Add_Multiply (int int1, int int2) 11 {12 var KeyValuePair = new KeyValuePair <int, int> (int1 + int2, int1 * int2); 13 return KeyValuePair; 14}View Code
- Use ref/out parameters
- Ref
- 1 static void Main (string [] args) 2 {3 int int1 = 15; 4 int int2 = 25; 5 int add = 0; 6 int multiply = 0; 7 Add_Multiply (int1, int2, ref add, ref multiply); 8 Console. writeLine (add); 9 Console. writeLine (multiply); 10} 11 12 private static void Add_Multiply (int int1, int int2, ref int add, ref int multiply) 13 {14 add = int1 + int2; 15 multiply = int1 * int2; 16}View Code
- Out
- 1 static void Main (string [] args) 2 {3 int int1 = 15; 4 int int2 = 25; 5 int add = 0; 6 int multiply = 0; 7 Add_Multiply (int1, int2, out add, out multiply); 8 Console. writeLine (add); 9 Console. writeLine (multiply); 10} 11 12 private static void Add_Multiply (int int1, int int2, out int add, out int multiply) 13 {14 add = int1 + int2; 15 multiply = int1 * int2; 16}View Code
- Use struct or class
- Struct
- 1 struct Result 2 {3 public int add; 4 public int multiply; 5} 6 static void Main (string [] args) 7 {8 int int1 = 53; 9 int int2 = 17; 10 var result = Add_Multiply (int1, int2); 11 Console. writeLine (result. add); 12 Console. writeLine (result. multiply); 13} 14 15 private static Result Add_Multiply (int int1, int int2) 16 {17 var result = new Result18 {19 add = int1 + int2, 20 multiply = int1 * int221}; 22 return result; 23}View Code
- Class
- 1 class Result 2 {3 public int add; 4 public int multiply; 5} 6 static void Main (string [] args) 7 {8 int int1 = 13; 9 int int2 = 27; 10 var result = Add_Multiply (int1, int2); 11 Console. writeLine (result. add); 12 Console. writeLine (result. multiply); 13} 14 15 private static Result Add_Multiply (int int1, int int2) 16 {17 var result = new Result18 {19 add = int1 + int2, 20 multiply = int1 * int221}; 22 return result; 23}View Code
- Dynamic
- 1 static void Main (string [] args) 2 {3 int int1 = 13; 4 int int2 = 27; 5 var result = Add_Multiply (int1, int2); 6 Console. writeLine (result. add); 7 Console. writeLine (result. multiply); 8} 9 10 private static dynamic Add_Multiply (int int1, int int2) 11 {12 var result = new13 {14 add = int1 + int2, 15 multiply = int1 * int216}; 17 return result; 18}View Code
- Use Tuple
- 1 static void Main (string [] args) 2 {3 int int1 = 25; 4 int int2 = 28; 5 var result = Add_Multiply (int1, int2); 6 Console. writeLine (result. item1); 7 Console. writeLine (result. item2); 8} 9 10 private static Tuple <int, int> Add_Multiply (int int1, int int2) 11 {12 var tuple = new Tuple <int, int> (int1 + int2, int1 * int2); 13 return tuple; 14}View Code
Okay: A bit more nonsense. Let's take a look at the writing method in C #7.0.
New features (C #7.0)
Old rules: first code
1 static void Main(string[] args) 2 { 3 int int1 = 25; 4 int int2 = 28; 5 var result = Add_Multiply(int1, int2); 6 Console.WriteLine($"Add: {result.add}, Multiply: {result.multiply}"); 7 //(var add, var multiply) = Add_Multiply(int1, int2); 8 //Console.WriteLine($"Add: {add}, Multiply: {multiply}"); 9 }10 public (int add, int multiply) Add_Multiply(int int1, int int2) 11 => (int1 + int2, int1 * int2);
How is it? Is there a fresh feeling compared to 6.0 and the previous C.
In fact, it's just the syntax sugar that simplifies the syntax based on Tuple, but it just gives people the illusion of multiple return values.
Summary
Although this feature is not an exciting change, it solves some of the itch points of many coders.
1. Look at the KeyValue pair method. This is a simple operation. The code written is very clumsy and can be obtained based on the Key when the value is set. In addition, the most important thing is that the Code called outside will not know the keys if it is not running.
2. Besides, Ref/Out is the most popular method in the traditional sense. Even this feature of C #7.0 cannot ban the nature of ref in certain scenarios. But at least in the case that ref is used to return values, the style of the Code is obviously different from the actual logic. It is clear that the return value is used, but it is unreasonable to enter and exit in the form of parameters.
3. The methods of struct and class are not much said. If you are targetingOneEntity, but if the purpose is to return multiple data with little relevance, add a useless Model class or structure for inter-method transmission, it can only be said that it was helpless under the current solution. Although dynamicRepresentationThere is no such problem, but there is a more pitfall problem, unless at runtime, otherwise the external call code does not know what is passed in the method.
4. Speaking of the traditional Tuple, it is actually the closest to this feature, but let's look at the *. Item1, *. Item2 in the call .. Heaven knows what it is. Even in the implementation method, people are confused by the value that the type does not actually mean.
Finally, I will explain that C #7.0 has not yet been officially released. If you want to experience some features, you can download the VS15 preview version, the syntax for the final release may be different from the one mentioned in this Article. For the latest developments, please pay attention to the Roslyn project.