C# 9.0 Features

C# 9.0 Features

Record Types

Records is a new reference type that you can create instead of classes or structs. Records should be viewed more as “values” data and less as objects. They are not meant to have mutable encapsulated state.

Features

  • New reference type
  • Immutable by design
  • Save us from writing boilerplate code
  • Generated as a class in IL

Reserved word "record"

To create a record you can use the record word as follow:

No hay texto alternativo para esta imagen

Positional records

It is possible to specify your own constructor and deconstructor in a record

No hay texto alternativo para esta imagen

A shorter syntax to express exactly the same

No hay texto alternativo para esta imagen

It can be used as follows

No hay texto alternativo para esta imagen

Value-based equality

A record is equal to another record when the property values match and they are of the same type. In this example, the comparison of the two records is true because they have the same value and are EventPriceType

No hay texto alternativo para esta imagen

Instead, this next comparison would be false because the records, despite having the same value, are of different types.

No hay texto alternativo para esta imagen

With-expressions | Cloning

When coping a record type, one way to change some of the values easily is by using the word with. In this example eventPrice2 will have the same value as eventPrice1 with two fields changed: Name and TickePrice.

No hay texto alternativo para esta imagen

Data members

In records, instead of an implicitly private field, as in other class and struct declarations, it is taken as an abbreviation of public, ini-only and auto-property.

No hay texto alternativo para esta imagen

The following code is the same as we had before

No hay texto alternativo para esta imagen

With-expressions and inheritance

A record can inherit from another record. However, a record cannot inherit from a class and a class cannot inherit from a record. The following code shows an example of inheritance with records.

Declaring the records

No hay texto alternativo para esta imagen

It can be used as follows

No hay texto alternativo para esta imagen

Init only properties

The init accessor is a variant of the set accessor which can only be called during object initialization. The great advantage of this init accessor is that it can initialize an immutable property. 

For a User class

No hay texto alternativo para esta imagen

Can be initialized with the following code:

No hay texto alternativo para esta imagen

An attempt to change the value of one of these properties after initialization results in a compiler error

No hay texto alternativo para esta imagen

Init accessors and readonly fields

The init accessor can be used with readonly fields.

No hay texto alternativo para esta imagen

Top-level statements

In C# 9 the main program can be written with less code.

No hay texto alternativo para esta imagen

Instead, you can choose to write your main program at the top level :

No hay texto alternativo para esta imagen

Pattern matching enhancements

Relational Pattern

C# 9 introduces patterns corresponding to the relational operators <, <=, > and so on.

The next if clause shows how to use a >= relational operator:

No hay texto alternativo para esta imagen

This operators can also be used in a switch as follows:

No hay texto alternativo para esta imagen

Logical Pattern

You can also combine patterns with logical operators: and, or and not.

No hay texto alternativo para esta imagen

The not operator can be used in an if statement instead of using !

No hay texto alternativo para esta imagen

Covariant returns

C# 9 allows you to return a more specific type than the declaration in the base type.

No hay texto alternativo para esta imagen

In this example, the Animal class (the base class) has a GetFood method that returns Food type

No hay texto alternativo para esta imagen

But the GetFood method of the Tigger class (which inherits from Animal) returns a Meet type that inherits from Food

No hay texto alternativo para esta imagen


To view or add a comment, sign in

Others also viewed

Explore content categories