C# 8 officially got released on 23rd Sep 2019 along with .NET Core 3.0 and Visual Studio 2019 v16.3. It has several new features and enhancements to give more power to developers and make their day to day coding even easier.
I have started writing a series of articles to go through the new C# 8 features one by one to cover all the new features and enhancements. In case you have not gone through my previous articles on C# 8, you may go through as follows.
- Working With Default Interface Implementation In C# 8.0
- Working With Ranges And Indices In C# 8.0
- Working With Switch Expressions And Pattern Matching In C# 8.0
In this article, we will go through the Nullable reference types.
So far only value types had nullable types and starting C# 8, even reference types can be declared as nullable types.
Traditionally developers considered that reference type is meant to accept non-null and null both and there wasn't any explicit handling required and unfortunately this consideration is one of the primary root causes of famous "Null Reference Exception".
The primary reason for NullReferenceException is that reference in question doesn't have any valid memory address and hence while de-referencing (reading value from memory address) it doesn't know from where to read the value and hence fails.
Categorizing reference types into nullable and non-nullable helps to clear the intent during the design phase and now reference types are by default defined as non-nullable and to make them nullable, explicit null casting (i.e.?) is required.
Non-nullable reference types don't need to go through a null-check before dereferencing (i.e. reading data from memory) however it's required for nullable reference types
and compiler issues warning, in case the value is known to be null.
Nullable Context Overview
Nullable contexts are defined values for the compiler to interpret reference type variables. There are two types of Nullable contexts as follows.
- The nullable annotation context: It can be enabled or disabled for any given source line. You can consider the pre-C# 8 compilers for the disabled annotation context.
- The nullable warnings context: It may be set to enabled, disabled, or safeonly. The nullable warnings context specifies the warnings generated by the compiler using its flow analysis.
The nullable annotation and warning context can be set for a project using the Nullable element in your csproj file. These settings define how the compiler interprets the nullability of types and what warnings are generated.
Nullable context is set in csproj file as follows.
- <LangVersion>8.0</LangVersion>
- <Nullable>enable</Nullable>
The following table illustrates the configuration of the nullable annotation and warning context are based on Nullable attribute in csproj file.
|
Nullable element value
|
Nullable annotation context
|
Nullable warning context
|
|
enable
|
enabled
|
enabled
|
|
disable
|
disabled
|
disabled
|
|
safeonly
|
enabled
|
safeonly
|
|
warnings
|
disabled
|
enabled
|
|
safeonlywarnings
|
disabled
|
safeonly
|
Now, let's understand the meaning of these values in the context of annotation and warning.
When Nullable element value is set as enable, a nullable annotation context is enabled and nullable warning context is enabled. As a result, reference type variables (for example string) becomes non-nullable. All nullability warnings are enabled.
In case, Nullable element value is set as disable, both nullable annotation context and nullable warning context is disabled. It essentially means that you are working in the pre- C# 8 version. All nullability check and warnings are disabled.
If you set Nullable value as safeonly, you basically set a nullable annotation context as enabled and nullable warning context as safeonly. It essentially means that variables of a reference type are non-nullable and only safety nullability warnings are enabled.




Sourav Kumar DasPosted Oct 18, 2019, 12:21 AM
Nice Article Sir.
Prakash TripathiPosted Oct 15, 2019, 2:18 AM
Thanks Pankaj Patel for reading.
Pankajkumar PatelPosted Oct 15, 2019, 12:12 AM
Nice article