Our Blogs

Microsoft Nullable Reference Types in C# 8 Previewed With New Features

November 17, 2017
61 Views
Microsoft Nullable Reference Types in C# 8 Previewed With New Features
Microsoft has announced the addition of nullable reference types in C# 8. The Redmond-based software giant has previewed the nullable reference types to enable developers to provide feedback. The preview is available as a Roslyn extension for Visual Studio 2017 15.5 Preview 4+ and above. While the .NET Framework projects are supported by default, the .NET Core support will be made available within the next few days.

The main purpose of the Nullable Reference Type in C# 8 is to mainly solve the mistake made by a scientist while working with null references. In 1965, Tony Hoare committed the mistake while working on ALGOL programming language. The pointers could be null but it is not expected from the framework. It has been proved that null pointers are a major source of bugs over an extended period of time.

The Add non-nullable reference types in a C# feature was suggested on User Voice in 2011 but was pushed to the 15th slot. interestingly, the same feature is the latest to be introduced on User Voice. The reason for the delay in the implementation is due to the fact that C# make use of null references everywhere.

Commenting on the development, Mads Torgersen, Lead Designer of C# disclosed that the null references are the primary default value of every reference type. He also added that null is a sensible value. Moreover, a field doesn't have a value.

By default, Microsoft has selected to consider reference types as non-nullable instead of non-nullable reference types in C#. It also provides a comprehensive mechanism to deal with nullable types. Torgersen added that nullable reference types are rate and should require a new annotation.

Basically, a nullable reference type is defined with "?" symbol adjacent to public string as shown below

class Person {
   public string FirstName; 
   public string? MiddleName; // May be null
   public string LastName;
}

You should note that the first and last statements are not null. The null identifiers can be easily identified by ? symbol. Microsoft is currently accepting feedback from developers to test and provide a detailed feedback regarding its usage and working.