• Fri. Jan 10th, 2025

C# 11 previews raw string literals, dumps parameter null checking

Byadmin

Apr 21, 2022


Microsoft’s planned C# 11 language upgrade is set to feature capabilities such as raw string literals and checked user-defined operators, along with better performance.C# 11 is due to arrive as a production release with the .NET 7 software development platform in November. Runtime performance is improved in C# 11 by caching of static method groups, rather than creating fresh delegate instances. This will improve application performance particularly for ASP.NET. Developers get the benefit of this feature with no effort on their part.Raw string literals in C# 11, which start and end with three double-quotes (“””), promise to make it easier to work with literals that contain characters that would otherwise need to be escaped. Raw string literals have no escaping, so all characters are considered content and included in the string. The sequence t is output as a backslash and the character t, not as the tab character.With checked user-defined operators, C# 11 adds the ability to declare certain operators as checked, marked with the checked modifier. Operators lacking this modifier will be unchecked when paired with a checked operator. This capability is important to developers building user-defined operators that operate on types where arithmetic overflow is a valid concept.One feature previously slated for C# 11, parameter null checking, has been removed. Microsoft said it now lacks sufficient confidence that it had the right feature design for C#, according to an April 18 blog post. The feature would allow !! on the end of a parameter name to provide parameter null checking before the method begins execution.C# is positioned as a modern, type-safe, object-oriented language for building secure applications to run on .NET. Developers can access new C# 11 features by downloading previews of Visual Studio or .NET 7. Other capabilities eyed for C# 11 include:
For UTF-8 character encoding, C# 11 enables conversion of string literals containing only UTF-8 characters to their byte representation. UTF-8 is used in many scenarios, particularly on the web. Prior to C# 11, developers had to either translate UTF-8 into hexadecimal, leading to verbose, unreadable, error-prone code, or encode string literals at runtime.
Developers can pattern-match a Span<char> or a ReadonlySpan<char> with a string literal.
With auto-default structs, the compiler initializes any fields and auto-properties that are not set based on definite assignment rules, and assigns default values. This capability simplifies initialization for anyone using structs that include explicit constructors.
A Warning Wave 7 is being introduced that includes a warning for any type that is declared with all lower-case ASCII letters. This is being made a warning because C# occasionally needs to introduce new keywords to evolve.
Previously, Microsoft revealed C# 11 improvements such as list patterns. Predecessor C# 10 arrived last November, featuring a compiler intended to better understand code and produce fewer errors.

Copyright © 2022 IDG Communications, Inc.



Source link