In the past month, from Microsoft Build, there have been several essential announcements that include but are not limited to .NET 5, Blazor WebAssembly, .NET MAUI, WinUI, and much more. A bunch of good news for anyone involved in C# development.
Be it a C# development company or a C# programmer. However, most programmers are eager to get their hands on C# 9, which proposes many exciting new features. It is time to take a quick tour since the new version is here.
The list is long; hence we will highlight the most interesting ones.
There is no doubt that the new features are currently under development, and there will be some of the other variations until the final release is due.
C# 9.0 is out to be part of the .NET 5 development platform, which is set to arrive in November this year. The C# 8.0 version arrived last September. Some of the new features in C# 9.0 were detailed at Build last month by C# lead designer Mads Torgersen; they include the following:
Init-only properties come with this version. Microsoft introduces an init assessor which is a variant of the set assessor. This will be for calling during object initialization. The properties address a limitation of object initializers, where properties must be mutable for these initializers to work.
Improved target typing is a term that describes when an expression gets its type from the context used. Some expressions which were not previously target-typed, will be guided by their own context.
Target-typed new expressions are included in this version in which the type can be ignored if there is a transparent type that the expression is being assigned to.
C# 9.0 has Value-based equality. All the objects inherit a virtual Equals(Object) method from the Object class. One can use this as the foundation for the Object.Equals(Object, Object) static procedure when both parameters are non-null. Structs override this to have “value-based equality,” they compare each field of the struct by calling Equals on them recursively. Records do the same.
Data members- records are overwhelmingly designed to be immutable, with init-only public properties that can be non-destructively transformed through with-expressions. In order to optimize for that typical case, records modify the defaults of simple member declaration of the form string FirstName means. Rather than having an implicitly private field, considering class and struct declarations, in records, this is taken to be shorthand for a public, init-only auto-property!
One of the few things that you will see come up from a developer that has recently switched from Visual Basic to C# is the switch statement. Despite C# undergoing nearly nine complete revisions, support for comparison operations within switch statement was never supported (although pattern matching comes close), this proposal aims to remedy that.
Similar to discriminated union in F# (a functional / Object-Oriented hybrid language of .NET world), this feature is connected to the previous ones and offers a way to define and use data types that can contain any number of different types.
Covariant return- As of now, the time when you override a method from a base class, the overriding procedure must return the same type as the base class method. In a few situations, it would be more practical to return a more specific type. C# 9 makes this possible by allowing overriding methods to return a type derived from the base method’s return type
Positional records provide a method to records where contents are provided via constructor arguments and can be extracted with the help of positional deconstruction.
We have top-level programs, to address the issue of too much boilerplate code.
Non-destructive mutation. Working with immutable data, the typical pattern is to create new values from existing values that represent a new state; For Example in Person object, if we want Object with different last name, we would have a new person object with copying properties from existing person object with only different last name.
C# 9.0 allows this is using “with-” expression. It uses object initializer syntax to state what is different in the new object form old object. Behind the scene record class implicitly defines a protected copy constructor which takes the original Object to copy values
Conclusion
C# is a very robust language that supports projects for everyone from startups to large enterprises. Over the years it has come at a place where anyone can trust the programming language for agility and robustness. Now is the best time to invest and hire C# developers to start building state-of-the-art software applications and web applications.