The article Introducing C# 10 by Ken Bonny discloses some incremental but very welcome changes to the C# language in the iteration that will be released with .NET 6 in November.
In no particular order:
field
in property accesses to manipulate the backing property without having to define it. This is a welcome improvement that will clean up useless boilerplate for properties that need to do something with the value before storing it (e.g. field.Trim()
)required
keyword for properties in any of the supported types (e.g. records
, classes
, structs
, or struct records
). This lets types enforce initialization without forcing a constructor parameter. The compiler will force callers to initialize the property in the object initializer instead.record struct
for records that are value instead of reference typesoperator
overloads in records
with
operator will work with anonymous classes as well as declared types.global usings
for commonly used namespaces (e.g. System
) to cut down on clutter in filesnamespace
without braces will put all types in that file into that namespace. This cuts down on an indenting level in all files.interfaces
(to round out the default-implementation feature introduced in C# 9)strings
(e.g. $”Hello {Name}”
is considered constant if Name
is also considered constant (recursively, of course). Update on November 11th, 2021 from Dissecting Interpolated Strings Improvements in C# 10 by Sergey Teplyakov (Dissecting the Code): This feature is based on an a nice performance improvement, as well. The compiler now understands interpolated strings and emits more efficient code rather than always using string.Format()
, which incurred allocations for unboxing, time for parsing, etc. There are even attributes to hook the compiler output that could be e.g, “used by logging frameworks to avoid string creation if the logging level is off.”!!
suffix for method arguments that instructs the compiler to generated a null-check for that argument. So, string
is not nullable, but not checked (i.e. the developer is responsible for including a check to avoid a NullReferenceException
if one slips past the compiler), string?
is nullable, and string!!
is not nullable and checked. This will avoid a ton of boilerplate argument-checks. Can’t wait.var
to declare variable to which you assign a manifest lambda. E.g. var isEven = (int n) => n % 2 == 0;
automatically gets the type Func< int, bool>
.(x3, int y3) = p;
where x3
is a preexisting variable.I really appreciate how the changes build on changes that came in previous versions. There’s a very noticeable direction that they’re pulling in with these languages changes:
For more information, see the csharplang/proposals/ (GitHub) folder. Some of the C# 10 features are in the main folder rather in the csharp-10.0/
folder.