You are on page 1of 11

INTRODUCTIO

N TO .NET 5 &
C# 9.0
FEATURES
Internal Use - Confidential
HISTORY
The birth of the project
Microsoft technical team met as a in December 2018 in Boston to kick off this project. Design leaders
from .NET teams (Mono/Xamarin and .NET Core) and also from Unity presented on various technical
capabilities and architectural direction.
We are now moving forward on this project as a single team with one set of deliverables. Since December, we
have made a lot of progress on a few projects:
Defined a minimal layer that defines the runtime <-> managed code layer, with the goal making >99% of
CoreFX common code. MonoVM can now use CoreFX and its class libraries. Run all CoreFX tests on
MonoVM using the CoreFX implementation. Run ASP.NET Core 3.0 apps with MonoVM. Run
MonoDevelop and then Visual Studio for Mac on CoreCLR.
C# 9.0

 Records
 Init-only properties
 Top-level statements
 Pattern matching enhancements
 Fit and finish features

Internal Use - Confidential


Records

 Implement System.IEquatable<T>
 Reference types
 Immutable by default (none of the properties can be modified once it's been created)
 e.g. –
public record Person
{
public string LastName { get; }
public string FirstName { get; }

public Person(string first, string last) => (FirstName, LastName) = (first, last);
}

 Supports inheritance
 Seal records to prevent further derivation
 Support copy construction
 Records can be copied with modification. These copy and modify operations supports non-destructive mutation
 Supports with expression (creates n copies by deriving with expression)
 Value based equality.

Internal Use - Confidential


Init-only properties

 can only be called during initialization


 allowed to mutate readonly fields of the enclosing class
- public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}

Internal Use - Confidential


Top-level statements

 remove unnecessary ceremony from many applications


 Example : canonical "Hello World!" program
- using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Can be written in single statement –
System.Console.WriteLine("Hello World!");
Internal Use - Confidential
Top-level statements

 one file in an application may use top-level statements


 common uses for this feature is creating teaching materials
 enable a script-like experience for experimentation similar to what Jupyter notebooks provide
 great for small console programs and utilities
 Azure Functions are an ideal use case for top-level statements
 don't limit your application’s scope or complexity
 can access or use any .NET class

Internal Use - Confidential


Pattern matching enhancements

 Type patterns match a variable is a 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.

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

Internal Use - Confidential


conditional ?? and ?: expressions don’t have an obvious shared type between the branches

Fit and finish features

 can omit the type in a new expression when the created object's type is already known
- private List<WeatherObservation> _observations = new();
 Target-typed new can also be used when you need to create a new object to pass as an argument to a method
  improves the target type resolution of conditional expressions, to overcome on problem of conditional ?? and ?:
expressions don’t have an obvious shared type between the branches
 apply attributes to local functions
 Covariant returns - to express that a method override in a derived class has a more specific return type than the
declaration in the base type
 new syntax for a null/not null check
if(obj is null) // null check
if(obj is not null) // not null check

Internal Use - Confidential


Support Code Generators
 partial method syntax –
Before C# 9.0, partial methods are private but can't specify an access modifier, have a void return, and can't have out
parameters. These restrictions meant that if no method implementation is provided, the compiler removes all calls to the
partial method. C# 9.0 removes these restrictions, but requires that partial method declarations have an implementation.
Code generators can provide that implementation. To avoid introducing a breaking change, the compiler considers any
partial method without an access modifier to follow the old rules. If the partial method includes the private access
modifier, the new rules govern that partial method
 module initializers -
Module initializers are methods that have the ModuleInitializerAttribute attribute attached to them. These methods will be
called by the runtime before any other field access or method invocation within the entire module. A module initializer
method:
Must be static
Must be parameterless
Must return void
Must not be a generic method
Must not be contained in a generic class
Must be accessible from the containing module
- the method and its containing class must be internal or public. The method can't be a local function.

Internal Use - Confidential

You might also like