Nullable reference types most impactful C# 8.0 feature, says Microsoft
Capability introduced in C# 8.0 promises to help developers eliminate common, difficult-to-debug null reference exceptions
3 December 2019 | 0
Nullable reference types, featured in Microsoft’s C# 8.0 language, hold developers to a higher standard when dealing with nulls, says Microsoft’s Mads Torgersen, lead designer for the C# language.
Described by Torgersen in a recent blog post as “probably the most impactful feature of C# 8.0,” nullable reference types allow developers to make the flow of nulls explicit in their code and warns them when they do not act according to their intent. Null reference types will issue new warnings on existing code, so they are an optional feature that must be explicitly enabled. Once enabled in code, developers will have to make the code null-safe to pass the compiler.
Until C# 8.0, which became officially available in late September with the arrival of .NET Core 3.0, C# has allowed references to be null and also allowed them to be dereferenced without checks, leading to
NullReferencesException exceptions when nulls are accidentally dereferenced. An undesired null from one place in the code can lead to an exception being thrown from a different place that dereferences it, thus making null bugs difficult to find and fix.
By being stricter with nulls in C# 8.0, Microsoft seeks to eliminate this issue. Warnings will be issued when values of ordinary reference types, such as
string, object, and
IDisposable, are null. But developers must turn on the null reference type feature to get new warnings on existing code, either at the project level or directly in the source code, with a new directive,
Torgersen suggests that developers consider the next year, until the planned November 2020 rollout of .NET 5, as the nullable rollout phase. Afterward, using null reference types everywhere should be the “new normal.” Authors of libraries and infrastructure such as code generators are encouraged to adopt null reference types during the rollout phase.
IDG News Service