Open Menu dzosoft
Close Menu dzosoft

   ALL ABOUT COMPUTER SCIENCE AND TECHNOLOGY


                             




Dazzle with your smile!

Publish perfectly-optimized content in 1-click









 
 
 

C# Version 9.0

 
 
C# Version 9.0
 

Released November, 2020


C# 9 was released with .NET 5. It's the default language version for any assembly that targets the .NET 5 release. It contains the following new and enhanced features:

Records
Init only setters
Top-level statements
⮚Pattern matching enhancements: relational patterns and logical patterns
Performance and interop
     ⮚Native sized integers
     ⮚Function pointers
     ⮚Suppress emitting localsinit flag
     ⮚Module initializers
     ⮚New features for partial methods
Fit and finish features
     ⮚Target-typed new expressions
     ⮚static anonymous functions
     ⮚Target-typed conditional expressions
     ⮚Covariant return types
     ⮚Extension GetEnumerator support for foreach loops
     ⮚Lambda discard parameters
     ⮚Attributes on local functions

C# 9 continues three of the themes from previous releases: removing ceremony, separating data from algorithms, and providing more patterns in more places.

Top level statements means your main program is simpler to read. There's less need for ceremony: a namespace, a Program class, and static void Main() are all unnecessary.

The introduction of records provides a concise syntax for reference types that follow value semantics for equality. You'll use these types to define data containers that typically define minimal behavior. Init-only setters provide the capability for non-destructive mutation (with expressions) in records. C# 9 also adds covariant return types so that derived records can override virtual methods and return a type derived from the base method's return type.

The pattern matching capabilities have been expanded in several ways. Numeric types now support range patterns. Patterns can be combined using and, or, and not patterns. Parentheses can be added to clarify more complex patterns:

C# 9 includes new pattern matching improvements:

⮚Type patterns match an object matches a particular type
⮚Parenthesized patterns enforce or emphasize the precedence of pattern combinations
⮚Conjunctive and patterns require both patterns to match
⮚Disjunctive or patterns require either pattern to match
⮚Negated not patterns require that a pattern doesn't match
⮚Relational patterns require the input be less than, greater than, less than or equal, or greater than or equal to a given constant.


These patterns enrich the syntax for patterns. Consider these examples:

public static bool IsLetter(this char c) =>
    c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';


With optional parentheses to make it clear that and has higher precedence than or:

public static bool IsLetterOrSeparator(this char c) =>
    c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.' or ',';


One of the most common uses is a new syntax for a null check:

if (e is not null)
{
    // ...
}


Any of these patterns can be used in any context where patterns are allowed:

is

pattern expressions, switch expressions, nested patterns, and the pattern of a

switch

statement's

case

label.

Another set of features supports high-performance computing in C#:

The

nint

and

nuint

types model the native-size integer types on the target CPU.
Function pointers provide delegate-like functionality while avoiding the allocations necessary to create a delegate object.
The

localsinit

instruction can be omitted to save instructions.

 

Performance and interop

 

Another set of improvements supports scenarios where code generators add functionality:

Module initializers are methods that the runtime calls when an assembly loads.
Partial methods support new accessibly modifiers and non-void return types. In those cases, an implementation must be provided.

 

Fit and finish features

 

C# 9 adds many other small features that improve developer productivity, both writing and reading code:

⮚ Target-type

new

expressions

static

anonymous functions
⮚ Target-type conditional expressions
⮚ Extension

GetEnumerator()

support for

foreach

loops
⮚ Lambda expressions can declare discard parameters
⮚ Attributes can be applied to local functions

The C# 9 release continues the work to keep C# a modern, general-purpose programming language. Features continue to support modern workloads and application types.

Leave comment
          

Save nickname and email in this browser for the next time.



Loading...     
close publicity
TRAIN INTENSELY LIKE NEVER BEFORE AND PUSH YOUR LIMITS!