The 5 Most Anticipated Features of C# 9

 

One of this year’s most anticipated developments is C# 9, which will rejoice the programmer community with new, exciting features. Even though there are no concrete facts or declarations about which features will be incorporated in the final release, we can already talk about the most popular proposals based on this GitHub list of Language Version Planning for C# and the feature ideas suggested by the community.

 

In this article, we are going to introduce and detail the 5 most anticipated features of C# 9, which are the following:

 

  1. Simplification of Parameter null validation

  2. Primary constructions

  3. CallerArgumentExpressions

  4. Target-Typed “New”

  5. Discriminated Unions

     

Let’s explore them one by one!

 

Simplification of Parameter null validation


Writing countless null checks can be tedious. Moreover, building lines of if statements with nested exceptions are not at all aesthetic. However, the simplified null validation proposal would allow coders to eliminate the need for explicit null checking by adding the “!” operator to the parameter, which will trigger the C# compiler to give off a standard “null” checking code. This feature would potentially make the argument validation visible to upstream callers via IntelliSense. Read more details about this
code enhancement proposal.

For example:


Primary constructions

Another handy proposal, which has been the topic of discussions since the release of C# 6, is the idea of primary constructions. This feature would allow programmers to make class constructions simpler just like in TypeScript, Kotlin, and other languages. Primary constructions would aim to reduce the overhead by putting constructor arguments directly in scope throughout the class. 

 

For instance:

 

Code without Primary constructions:

Code with primary constructions:

This method would result in a significantly shorter code, which also decreases the possibility of code-breaks and errors, as the code you don’t have to write won’t break. Furthermore, by eliminating redundant boilerplate code developers would be able to make their code files easier to skim through. Not to mention that these solutions ease their job when it comes to uncovering bugs.

 

 

CallerArgumentExpression

 

The new CallerArgumentExpression feature would come to your help in C# 9 when an assertion or an argument validation fails. When something like this happens, all coders want to know in as many details as possible where and why it occurred. However, in the current stack trace, with the use of Debug info in the code, it is only the file name, the line number, and the method number that is given to them, which is most of the time not enough to tell the assert that failed.

This is why the C# community proposed the CallerArgumentExpression to be included in the new C# 9 release. This would allow the caller to project the expressions passed in at a call site as a string. When using this solution, the constructor of the attribute will need to pick a string argument that specifies the name of the argument in order to project it as a string, in other words, to stringify it.

 

Developers could add the CallerArgumentExpression to Debug.Assert as follows, to get the requested information:

 

 

Target-typed “New”

One of the proposals of C# 9 would give a new, focused and time-saving usage for the word “new”. In the case of new type-targeted expressions, where you already know the type, you could totally forget about the type specifications for constructions. Since the type of expressions is already known, this new “New” would allow you to simplify their usage by entering “new()” instead of “new Dictionary<string, List<string>>()”.

 

 

Discriminated Unions

 

“Enum class”-es are a new kind of type declarations. They are usually referred to as Discriminated Unions, due to the fact that they list every single instance type, while the instances do not overlap with each other. To put it differently, discriminated unions are a great solution to define types that encompass any number of dissimilar data types. They are used to enumerate different values.

At the moment C# does not fully support discriminated unions, only via inheritance and some extent of overloading, though having concise and seamless support for these declarations should undoubtedly exist in this programming language, according to the majority of C# developers.

Conclusion

 

All things considered, it looks like C# gains its popularity by adapting to the requirements of developers and by making learning and adaptation easier for those programmers, who do not specialize in a single programming language. The 5 new feature proposals mentioned above clearly show the way how C# intends to support more functional programming techniques. These improvements make the life of developers easier, while they increase the number of the programming language’s fanbase.


Disclaimer:

Please note that the above-mentioned features are not guaranteed to be part of the new C# 9 release, though it is great to be on top of IT news and follow the course of the development.

It has not been revealed when the release will roll out. One thing's for sure, though: a major Visual Studio update will be needed for it.