C# 8.0 Nullable Reference types are here!

Also in this series

  1. C# 8.0 Nullable Reference types are here!
  2. Pattern matching in C#
  3. Asynchronous streams
  4. Indices and ranges
  5. Default interface methods
  6. 5 tips to improve your productivity in C# 8.0

All code samples are available on github

C# 8.0 just rolled out with plenty of new features. One of the most important is the support of nullable reference types (NRT). A bunch of words that don't seem to explain what it does. I mean, aren't all types (except value types) already nullable?

What Microsoft means by NRT is that now you can explicitly tell the compiler to check for null values. They do that by switching the default from null to not null for all types. So unless you explicitly specify it, you can assume that variables cannot be null anymore.

mindblown

Why do that? Well it's the first attempt by Microsoft to fix the dreaded NullReferenceException that has plagued so many applications. Many have even called the decision to allow null values in C# the billion-dollar mistake. You can also look at my other post (Why null in C# is so bad) for my take on the subject.

Let's try it

First of all, to try it, you must activate it. Either create a new application that runs on .net core 3.0 or open your .csproj and add this

<LangVersion>8.0</LangVersion>
<Nullable>Enable</Nullable>

<LangVersion>8.0</LangVersion> is to enable the C# 8.0 features and <Nullable>Enable</Nullable> is to switch the compiler behavior from all types null by default to all types not null by default.

You can also enable/disable this feature only for a specific block of code using these instructions

#nullable enable
#nullable disable
#nullable restore

The compiler will then run static analysis on your code and add a compilation warning when it detects that a value can be null in a specific code path.

i.e.

// warning CS8600: Converting null literal or possible null value to non-nullable type
string a = null;
// warning CS8602: Dereference of a possibly null reference
var l = a.Length;

New operators

This feature also introduces two new operators to deal with those cases.

  • ? to tell the compiler that this object can be null. This operator has the same syntax as the one used by the nullable value type. e.g. int?
  • ! a.k.a. the null forgiving operator is used to tell the compiler that we know that the value won't be null. The compiler will not check anymore for this variable. Be careful, though, because if you are wrong and the value is null at runtime, you'll get the good old NullReferenceException. Anyways, this is still very useful, because even the mighty c# compiler can be wrong and get mistaken while analyzing your code. Those false positives tend to happen when you are using features like reflection or deserialization.

Show me how to do it

If we go back to the previous block of code, we now have all the tools to get rid of those warnings. (Note that in this case, those warnings are legit, and you should handle them in another way. It's just an example to demonstrate how to use those operators.)

// this will make sure we can assign a null value to that variable
string? a = null; 

// this will remove the warning and let us try to call `.Length`
var l = a.Length;

Conclusion

NRT is a powerful tool that we'll all need to learn to use. It can bring us closer to correctness) with the elimination of some runtime problems by discovering them in the earliest stages of your Software Development Life Cycle (SDLC). This practice is also known as Shift Left. The idea is to raise issues sooner in the SDLC. A problem found during development costs way less that one found in production.

shiftleft

Also in this series

  1. C# 8.0 Nullable Reference types are here!
  2. Pattern matching in C#
  3. Asynchronous streams
  4. Indices and ranges
  5. Default interface methods
  6. 5 tips to improve your productivity in C# 8.0

All code samples are available on github

References

docs.microsoft.com/en-us/dotnet/csharp/null..

Learn Something New Everyday,
Connect With The Best Developers!

Sign Up Now!

& 500k+ others use Hashnode actively.

No Comments Yet