What is new in .net C# 8.0?

C#

C# 8.0 is here with some great new feature additions to the language. In this article, we will be going through the new features in.net C# 8.0. Before we start to dive into detail into each feature, let me give you the list of features:

Table of Contents

We will be breaking this list of features into multiple articles as a single article will be too long to explain each one of them:

So lets begin with number one:

Nullable reference types

As the name suggests, this change is related to reference type variables in the language. In short, this is just to express the intent that a reference variable can be null or not?.

So when you declare a reference variable, you can specify its expected value in terms of null or not null. With this new feature in .net C# 8.0, You can declare the reference variable by clearly specifying its compatibility with null.

Once you declare then C# 8.0 compiler will keep a watch on this variable throughout the program that it doesn’t violate its compatibility.

Some of the compatibility rules that are followed by the compiler are:

For Non-Nullable Reference:

  • The reference variable must be initialized to a non-null value.
  • The reference variable can never be assigned the value null.

For Nullable Reference:

  • A reference variable may be null. You can assign null while initializing or later in your code.

By doing this we get clarity on two things:

  • If a reference is not supposed to be null, then you can dereference it without any concern, because you know it won’t be null. so no risk.
  • If a reference is possibly null, then you can not dereference it without checking if it’s null or not. There is a risk.

What is Syntax to declare nullable or non-nullable

The operator? will be used to specify the compatibility of a reference variable with null.

This is the syntax to declare a reference as nullable type:

Syntax:

reference datatype? value
E.g: string? name;

This is the syntax to declare a reference as non-nullable type:

Syntax:

 reference datatype value;
E.g: string name;

Once you declare your reference type then the compiler will keep an eye using static code analysis and will make sure your variable is compatible.

If your code violates the compatibility then the compiler will prompt you with an error or warning.

For Example:

If your reference variable is a nullable type and you try to dereference it without doing a null check, the compiler will prompt you that there is a possibility of this variable to be null and cause a crash in your program.
Then you can correct your mistake.

Similarly, if your reference is a non-nullable type and you try to assign it will null. Then also compiler will prompt you to correct your mistake.

What is Nullable annotation context

In your program, you have control to enable or disable this feature.
You can enable/disable this feature for the entire project, a file, or a section. The coverage of this control is called the context.

Project level Context

If you disable this for the project then your context is the whole project where the feature is disabled.

File Level Context

If you disable this in a file of your code then your context is that file only where the feature is disabled.

What you gain or lose:

If Disabled:

1. You can’t declare nullable references in a disabled context.
2. All reference variables may be assigned a value of null.
3. No warnings are generated when a variable of a reference type is dereferenced.
4. The nullforgiving operator may not be used in a disabled context.
5. The behavior is the same as previous versions of C#.

If Enabled:

1. Any variable of a reference type is a non-nullable reference.
2. Any non-nullable reference may be dereferenced safely.
3. Any nullable reference type (noted by ? after the type in the variable declaration) may be null. Static analysis determines if the value is known to be non-null when it’s dereferenced. If not, the compiler warns you.
4. You can use the null-forgiving operator to declare that a nullable reference isn’t null.

How to Control this feature in your code?

You can control the type of prompt you want from the compiler:

1. At Project level:

In your project .csproj file, you can add below text:

1.Enable: This will enable this feature and will issue you warnings if you violate compatibility in your code.

<Nullable>enable</Nullable>

2. Disable: This will disable this feature and will not issue any warnings. Your existing code will work as it is.

<Nullable>disable</Nullable>

2.Directives to change it at code Level

You can also use directives to change the context configuration anywhere in your code or file.

nullable enable
nullable disable

nullable disable warnings: Set the nullable warning context to disabled.

nullable enable warnings: Set the nullable warning context to enabled.