9 New syntaxes in C #7,
I. out Variables
We have to do this before c #7.
In c #7, we can do this.
Of course, you can also use "var"
This is a small update. In fact, this problem has existed for a long time and should be well solved. I don't know why it was introduced in c #7. In any case, I can leave a few lines of code in the future.
Ii. Tuples)
Install the nuget package: System. ValueTuple
I personally think this is the most cool and most useful change brought by c #7. Let's look at it from the beginning.
Do you still remember "Tuple? Don't remember? OK. Let's take a look at its definition.
Let's see how to use it.
Do you want to cry .. To be honest, Tuple has never been used since self-programming. "ItemN" is very unfriendly to developers. I would rather create a struct or class to define the attribute.
Let's try it in c #7 (this also counts as many return values )?
If you think return (1, xxx) is not accurate enough, you can
If you do not want result. xxx, you can "deconstruct"
You can do this (not recommended)
You can also create a tuples temporarily in the method.
With these features, we can continuously declare and assign values to variables.
Deconstruct (not recommended)
3. Pattern matching)
This part mainly enriches the "switch", which is a bit similar to the exception filter of c #6.
In earlier versions, the switch can only be a case constant, but now more conditions are available.
In c #7, we can do this.
4. Local reference and return (Ref locals and returns)
This person is rarely used. He just writes a comparison code.
5. Local functions)
This is a very practical function, which is rich in action, and func. Let's look at an example:
Recursive call before c #7
In c #7
6. expressions can be used for More members (More expression-bodied members)
7. Throw an exception (Throw expressions)
8. asynchronous return of value type (Generalized async return types)
Install the nuget package: System. Threading. Tasks. Extensions.
Strictly speaking, this is not a new syntax feature, but adds some classes.
Before using "async" and "await", we must use Task as the return value (void is ignored in special cases), but Task is a reference type (class ), this will cause waste (memory and gc) in very simple tasks)
In c #7, Microsoft added an nuget package and added a new type "ValueTask" to solve this problem. It is a value type.
9. Numeric syntax improvement (Numeric literal syntax improvements)
Mom is no longer afraid of adding 0 more ~