3rd Jul 2021

Features of C# 9.0

.NET

Written By, Durga Bhimani

Overview

Hello Amigos,

 Hope you are aware of the newly released C#9.0 with some unified included features. With every new version, we aspire for great clarity and simplicity in code. In this blog, we are going to take a look at features with detailed examples of improved code, focus mainly on improvement and try to help developers with self-explanatory examples for easy understanding.

So let’s jump to the flow of this blog

1.Introduction of C# 9.0

There are tons of improvements and new features included in this version of C#. With every new version they are giving more clarity of code and giving developers more essential features to advance their so called “Good Code” and they are achieving it by outshining its simplicity. C# 9.0 is part of .NET 5, therefor C#9.0 is only supported on .NET 5.

Here is the list of new features available in C# 9.0 which is provided by Microsoft

  • Records
  • Init only setters
  • Top-level statements
  • Pattern matching enhancements
  • Native sized integers
  • Function pointers
  • Suppress emitting localsinit flag
  • Target-typed new expressions
  • static anonymous functions
  • Target-typed conditional expressions
  • Covariant return types
  • Extension GetEnumerator support for foreach loops
  • Lambda discard parameters
  • Attributes on local functions
  • Module initializers
  • New features for partial methods

Let’s discuss some of them.

2.Top Level Statement

Top level statement is an awesome feature that is introduced in C# 9.0 we didn’t find this feature in the before version of C#.

Before understanding the Top-Level statement let’s see the below code.

The above code is a simple Console application code.

Every C# project has an entry point and this is a Static main method.

In the previous version of C#, namespace and args[] are optional. We can run the program without namespace and args[].

Top Level statement allows you to write a program without namespace, class, parentheses, and the static main method. You can write your same code in just one line using a Top-level statement.

Where Is the entry point of the program?

As we know the main method is the entry point of every program in c# but in the Top-Level statement, there is no main method available So it is common that we have questions about where our program will start?

So the Top-level statement is a compiler feature. This means even if we didn’t write the Main method in our code then it will automatically generate the static main method in our program including args[] argument.

  • You can put a Top level statement in only a single file.
  • Because In Top-level statements it will generate automatically the main method and we can’t have multiple main methods in our program.
  • If we write top-level statements in more than one file then the compiler gets confused that from where the entry point is.

3.Init – Only Parameters

Before starting Init-only Property We should have basic knowledge of mutable and immutable in C#.

Mutable Object: As the name suggests mutable means something that can change. In c# mutable objects can be changed after their initialization.

Immutable Object: As the name suggests Immutable means something unable to change. in C# immutable means once you create an object it can not be changed if you want to change the object you have to create a new object or assign a new memory.

For example,

Here I created one Class BankAccountDetails. And it has 3 properties: AccountNumber, Name, Balance with get and set method.

Now I want to set and get value in this property.

Output is,
    AccountNumber = 123
    Name = Jone Doe
    Balance = 15000

This program is perfect but what if someone will try to change Account Details!

This is not reliable in a real-world application right? So that means we have to create the immutable property.

In the previous version, we can use a private set instead of the set for immutability. But here we have one problem that we can’t use object initializer instead of the constructor for outside the class, even not creating a new object. And This is also not possible in real-world applications because we have to access this property from outside the class and that’s not allowed with a private set property.

This was a really big problem for developers in work with C# till C# 8.0.

But Microsoft solves this problem in C# 9.0 by introducing init – only property.

Let’s understand with an example,

Here we can see that set is replaced with init method.

The output will be the same as the previous.

Init only property has power that it can set values according to your requirement. It will set data only at the initialization of the object or with the constructor. If you will not set data at initialization it will set automatically 0.

4.Records

C# 9.0 also introduced one new keyword “Record”.

It is quite similar to the class but Record is a reference type class. Record is by default immutable class means it is all properties are immutable.

Let’s understand with an example,

As mentioned in the above example if the class has all the immutable data then we can use records instead of class.

Here we can see that all the properties of the BankAccountDetails class are immutable. This means if we want to change the value of any property then we have to create a new object and then we can change the value.

Like below example,

But what if we have a thousand init-only properties and we need to change the value of only one or two data members.

So should I create a new object to change the value of only one property and copy all other properties’ values from the original object like in the above example?

No, This is not the right solution and for that C# 9.0 also introduced one more wonderful feature “With Expression”.

5.With Expression

We can only use “With expression” with a record.

Using “with expression” we don’t need to copy all properties’ values from the original object, we can only mention that property in the new object which we need to change.

Example is

But if we create a class instead of records then we can not use the “with expression” and we have to write all properties in a new object that’s why we should prefer a record instead of a class when all the data members are immutable.

Conclusion

I hope this blog is giving you a better understanding of the features of C# 9.0 with real-life examples and it will help you for better coding.

THANKS FOR READING

Written By,

.NET Developer at Yudiz Solutions Pvt. Ltd