VS2017 also RC several versions, this would like to follow up to see what content C#7 joined, to search C#7, did find a lot of articles, but many features IDE does not let the compiler ah ... So today we mainly list the c#7 characteristics that have been determined (generally speaking RC will not add new features, other preview features we may wait for c#8 haha)
Out variable
We have to do this before c#7.
We can do that in c#7.
Of course you can also use "var"
This is a small update, in fact, this problem exists for a long time, it should also be a good solution, I do not know why to c#7 before the introduction, no matter how later can be less a line of code.
Tuples (tuples)
Need to install NuGet package: System.valuetuple
Personally think this is the most cool and most useful change in c#7, we look at the beginning.
Do you Remember "Tuple"? Don't you remember? Ok let's take a look at its definition.
Take a look at using
Do you want to cry? To be true, since the self-programming has hardly ever used a tuple, "itemn" is very unfriendly to developers, I would rather create a struct or class to define the property.
Let's try it in c#7 (that's a lot of return values too)?
If you think return (1,XXX) is not accurate, then you can
If you don't want to result.xxx, you can also "deconstruct"
You can still do this (not recommended)
You can also create a tuple temporarily within a method
With these features we can also declare and assign variables in succession.
Deconstructed classes (not recommended)
Pattern matching (pattern matching)
This piece is mainly for "switch" is rich, somewhat similar to the C#6 exception filter
Switch can only have case constants in previous versions, and now it can have more conditions.
We can do that in c#7.
Local references and returns (Ref locals and returns)
This person is less used, simply write a comparison code
Native function (local functions)
This is a very useful feature, as is the richness of action, and Func, let's look at an example:
Recursive call before C#7
In the C#7
The expression can be used for more member (more expression-bodied members)
Expression throws an exception (throw expressions)
Asynchronous return of value type (generalized async return types)
Need to install NuGet package: System.Threading.Tasks.Extensions
In strict sense, this is not a new feature of syntax but adds some classes.
Before we wanted to use "async", "await" we had to work with the task as the return value (void special case ignored), but the task is a reference type (Class), which can cause waste in very simple tasks (memory and GC)
Microsoft added a NuGet package in c#7, adding a new type of "valuetask" to solve the problem. It is of value type.
Numeric syntax improvements (Numeric literal syntax improvements)
Mom's not afraid I'll add a 0.
Written in the last
Writing this is still a headache, because suddenly can't remember the application of this feature, or the need for concrete practice to understand the new syntax is useful.
It is worth noting that some of the new grammar is related to the NuGet package, which we will look at next time, mainly Roslyn credit.
Reference: Https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/csharp-7
Look c#7