📜 ⬆️ ⬇️

What prepares us for C # 7 (Part 1. Tuples)

There has not yet been an official release of C # 6 and its new compiler “Roslyn”, but the details of the next edition are already known - C # 7. And it promises us all sorts of “goodies” that should make our existence easier. Although this is all preliminary, but it is still interesting, what will please Microsoft in the not-so-near future.



Tuples (Tuples)


When you often use multiple return values ​​from methods, you usually use the out keyword in the method parameters or define various additional structures and classes. Usually they are located directly above the method definition or, in the case of variables, they need to be initialized somewhere before the call. This is not very convenient. So, what improvement is supposed, we will consider on an example of the following code:

public (int sum, int count) Tally(IEnumerable<int> values) { ... } var t = Tally(myValues); Console.WriteLine($"Sum: {t.sum}, count: {t.count}"); 

Here we see an anonymous structure definition with public fields. Those. we get a simple and quite convenient way to use the return of multiple values. What will happen inside is not very clear. Here is an example of working with the key async property:
')
 public async Task<(int sum, int count)> TallyAsync(IEnumerable<int> values) { ... } var t = await TallyAsync(myValues); Console.WriteLine($"Sum: {t.sum}, count: {t.count}"); 

With this new syntax, many interesting possibilities of creating anonymous types appear:

 var t = new (int sum, int count) { sum = 0, count = 0 }; 

This syntax seems too redundant. But the creation of a structure object using a literal seems very convenient:

 public (int sum, int count) Tally(IEnumerable<int> values) { var s = 0; var c = 0; foreach (var value in values) { s += value; c++; } return (s, c); //     } 

There is another method for creating an anonymous structure object using a literal:

 public (int sum, int count) Tally(IEnumerable<int> values) { var res = (sum: 0, count: 0); //         foreach (var value in values) { res.sum += value; res.count++; } return res; } 

This example very much reminded the creation of JSON. And while it is not entirely clear whether it will be possible to write something like this:

 var res = (sum: 0, count: 0, option :( sum: 0, count: 0)); 

But, as it seems to me, the most “goodness” is the creation of anonymous structures in collections.

 var list = List<(string name, int age)>(); list.Add("John Doe", 66); //          

We are glad about the openness of the development of the language today: everyone can influence the development and suggest their own ideas. More details about tuples here .

Update: Thank you, ApeCoder . He specified the absence in the article of the mechanism of variable initialization using tuples.
Here is an example:
 public (int sum, int count) Tally(IEnumerable<int> values) { var res = (sum: 0, count: 0); // infer tuple type from names and values foreach (var value in values) { res.sum += value; res.count++; } return res; } 

 (var sum, var count) = Tally(myValues); //   Console.WriteLine($"Sum: {sum}, count: {count}"); 

Source: https://habr.com/ru/post/256825/


All Articles