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:
Positional records
It is possible to specify your own constructor and deconstructor in a record
A shorter syntax to express exactly the same
It can be used as follows
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
Instead, this next comparison would be false because the records, despite having the same value, are of different types.
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.
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.
The following code is the same as we had before
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
It can be used as follows
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
Can be initialized with the following code:
An attempt to change the value of one of these properties after initialization results in a compiler error
Init accessors and readonly fields
The init accessor can be used with readonly fields.
Top-level statements
In C# 9 the main program can be written with less code.
Instead, you can choose to write your main program at the top level :
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:
This operators can also be used in a switch as follows:
Logical Pattern
You can also combine patterns with logical operators: and, or and not.
The not operator can be used in an if statement instead of using !
Covariant returns
C# 9 allows you to return a more specific type than the declaration in the base type.
In this example, the Animal class (the base class) has a GetFood method that returns Food type
But the GetFood method of the Tigger class (which inherits from Animal) returns a Meet type that inherits from Food