This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.

Title

C# 10 Features

Description

The article <a href="https://kenbonny.net/introducing-csharp-10" author="Ken Bonny">Introducing C# 10</a> 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: <ul> <c>field</c> 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. <c>field.Trim()</c>) The <c>required</c> keyword for properties in any of the supported types (e.g. <c>records</c>, <c>classes</c>, <c>structs</c>, or <c>struct records</c>). This lets types enforce initialization without forcing a constructor parameter. The compiler will force callers to initialize the property in the object initializer instead. <c>record struct</c> for records that are value instead of reference types <c>operator</c> overloads in <c>records</c> The <c>with</c> operator will work with anonymous classes as well as declared types. <c>global usings</c> for commonly used namespaces (e.g. <c>System</c>) to cut down on clutter in files <c>namespace</c> without braces will put all types in that file into that namespace. This cuts down on an indenting level in all files. Improvements to lambdas: attributes on parameters and return types, explicit return types Static methods on <c>interfaces</c> (to round out the default-implementation feature introduced in C# 9) Constant interpolated <c>strings</c> (e.g. <c>$"Hello {Name}"</c> is considered constant if <c>Name</c> is also considered constant (recursively, of course). <hl>Update on November 11th, 2021 from <a href="https://sergeyteplyakov.github.io/Blog/c%2310/2021/11/08/Dissecing-Interpolated-Strings-Improvements-In-CSharp-10.html" author="Sergey Teplyakov" source="Dissecting the Code">Dissecting Interpolated Strings Improvements in C# 10</a>:</hl> 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 <c>string.Format()</c>, which incurred allocations for unboxing, time for parsing, etc. There are even attributes to hook the compiler output that could be e.g, <iq>used by logging frameworks to avoid string creation if the logging level is off.</iq> <hl>Update on June 7th, 2021 from <a href="https://medium.com/young-coder/a-closer-look-at-5-new-features-in-c-10-f99738b0158e" source="Medium" author="Matthew MacDonald">A Closer Look at 5 New Features in C# 10</a>:</hl> Introduce <c>!!</c> suffix for method arguments that instructs the compiler to generated a null-check for that argument. So, <c>string</c> is not nullable, but not checked (i.e. the developer is responsible for including a check to avoid a <c>NullReferenceException</c> if one slips past the compiler), <c>string?</c> is nullable, and <c>string!!</c> is not nullable <i>and</i> checked. This will avoid a ton of boilerplate argument-checks. Can't wait. <hl>Update on November 4th, 2021 from <a href="https://thomaslevesque.com/2021/11/04/a-quick-review-of-csharp-10-new-language-features/" author="Thomas Levesque" source="">A quick review of C# 10 new language features</a>:</hl> The compiler will now <iq>[a]utomatically infer a “natural” type for a lambda</iq>, so you can now use <c>var</c> to declare variable to which you assign a manifest lambda. E.g. <c>var isEven = (int n) => n % 2 == 0;</c> automatically gets the type <c>Func< int, bool></c>. <hl>Also from the same November 4th article:</hl> You can now <iq>[m]ix declarations and variables in deconstruction</iq> so that you can now write <c>(x3, int y3) = p;</c> where <c>x3</c> is a preexisting variable. </ul> 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: <ul> Being able to write performant code (records, refs, etc.) Cutting down on boilerplate for common use cases (records, field, pattern-matching. etc.) Being able to write maintainable, backwards-compatible code (interface default methods, etc.) Improving type system (covariant returns, etc.) Turning runtime issues into compile-time issues (nullability, etc.) </ul> For more information, see the <a href="https://github.com/dotnet/csharplang/tree/main/proposals" source="GitHub">csharplang/proposals/</a> folder. Some of the C# 10 features are in the main folder rather in the <c>csharp-10.0/</c> folder.