We try out five new features in C# 9 that aren't in C# 8.
These include the following:
Target-Typed Object Creation
Init-Only Setters in Properties
Covariant Returns
Relational & Logical Patterns
Extended Partial Methods
C# 9 is expected to be released alongside .NET 5 at some point in 2020.
If you are using Visual Studio, you will need to install VS 2019 16.8 or higher for these to work. This may involve downloading the preview version of Visual Studio 2019.
Feature #1: Target-Typed Object Creation 0:57 - When declaring a new instance of a type, as long as you explicitly defined the type of the variable, you can just call the new keyword, without needing to define the type.
Feature #2: Init-Only Setters in Properties 2:44 - In a property, you can now replace the "set" with "init". What this allows you to do is to set the property only when you create a new instance of the type it belongs to. In previous versions of C#, if you wanted to set a property on initialisation, you would have had to pass the object into the constructor and set it that way. That alone adds more code.
Feature #3: Covariant Returns 4:02 - If you override a method, you can now return a type that inherits the original type. In previous versions of C#, you always had to return the same type, even if a type was a child of that type.
Feature #4: Relational & Logical Patterns 6:06 - The "switch" statement has had some enhancements with relational & logical patterns. When performing a switch statement, you can use the "greater than" and "more than" relational patterns. In addition, you can add logical patterns by using the "and" or "or" statements.
Feature #5: Extended Partial Methods 8:10 - Partial methods now have more functionality. You can now use accessors, like "public" or "private" and you can now return a type. This is an enhancement from C# 8 where you had use a partial method as private, and you could only return a void.
📖 Learn .NET and C# with our online courses 📖
https://www.roundthecode.com/dotnet-c...
#csharp #csharp9