C# 8.0 Nullable Reference types are here!
Tags: dotnet, csharp
Published: 2020-03-03 01:04:44

## Also in this [series](https://blog.miguelbernard.com/csharp-8-0-new-features/)

1. [C# 8.0 Nullable Reference types are here!](https://blog.miguelbernard.com/c-8-0-nullable-reference-types-are-here/)
2. [Pattern matching in C#](https://blog.miguelbernard.com/pattern-matching-in-csharp/)
3. [Asynchronous streams](https://blog.miguelbernard.com/asynchronous-streams/)
4. [Indices and ranges](https://blog.miguelbernard.com/c-8-0-indices-and-ranges)
5. [Default interface methods](https://blog.miguelbernard.com/what-are-default-interface-methods-in-c-8-0)
6. [5 tips to improve your productivity in C# 8.0](https://blog.miguelbernard.com/5-tips-to-improve-your-productivity-in-c-8-0)

> All code samples are available on [github](https://github.com/mbernard/csharp8sample)

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](https://blog.miguelbernard.com/content/images/2019/12/mindblown.jpg)

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](https://devblogs.microsoft.com/dotnet/nullable-reference-types-in-csharp/). You can also look at my other post ([Why null in C# is so bad](https://blog.miguelbernard.com/why-null-in-csharp-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
```xml
<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
```csharp
#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.
```csharp
// 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.)

```csharp
// 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](https://en.wikipedia.org/wiki/Correctness_(computer_science)) 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](https://blog.miguelbernard.com/content/images/2019/12/shiftleft.jpg)

## Also in this [series](https://blog.miguelbernard.com/csharp-8-0-new-features/)

1. [C# 8.0 Nullable Reference types are here!](https://blog.miguelbernard.com/c-8-0-nullable-reference-types-are-here/)
2. [Pattern matching in C#](https://blog.miguelbernard.com/pattern-matching-in-csharp/)
3. [Asynchronous streams](https://blog.miguelbernard.com/asynchronous-streams/)
4. [Indices and ranges](https://blog.miguelbernard.com/c-8-0-indices-and-ranges)
5. [Default interface methods](https://blog.miguelbernard.com/what-are-default-interface-methods-in-c-8-0)
6. [5 tips to improve your productivity in C# 8.0](https://blog.miguelbernard.com/5-tips-to-improve-your-productivity-in-c-8-0)

> All code samples are available on [github](https://github.com/mbernard/csharp8sample)

# References
https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references