-
Notifications
You must be signed in to change notification settings - Fork 17
/
C# 10 what's new;Note=Erxin.txt
70 lines (41 loc) · 1.87 KB
/
C# 10 what's new;Note=Erxin.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
C# 10 what's new;Note=Erxin
# Reference
- https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10
# New features
- improved structure types
using the record struct or readonly record struct declarations. You can now clarify that a record is a reference type with the record class
- interpolated string handler
create a type that builds the resulting string from an interpolated string expression. The .NET libraries use this feature in many APIs
- global using directives
the global modifier to any using directive to instruct the compiler that the directive applies to all source files in the compilation
- file scoped namespace delcaration
namespace MyNamespace;
- extended property patterns
+ before
{ Prop1: { Prop2: pattern } }
+ after
{ Prop1.Prop2: pattern }
- C# 10 includes many improvements to how lambda expressions
can have a natural type
can declare a return type
can apply attributes
- const strings may be initialized using string interpolatio
- record types can seal ToString
- assignment and declaration in same deconstruction
(x, int y) = point;
- improved definite assignment, no error
// Or, using ??
if (c?.GetDependentValue(out object obj) ?? false)
{
representation = obj.ToString(); // undesired error
}
- In C# 10 and later, you can specify a different async method builder for a single method
- You can use the System.Runtime.CompilerServices.CallerArgumentExpressionAttribute to specify a parameter that the compiler replaces with the text representation of another argument.
public static void Validate(bool condition, [CallerArgumentExpression("condition")] string? message=null)
{
if (!condition)
{
throw new InvalidOperationException($"Argument failed validation: <{message}>");
}
}
- C# 10 supports a new format for the #line pragma. You likely won't use the new format, but you'll see its effects.