Adventures in C#8 – The annotation for nullable reference types should only be used in code within a ‘#nullable’ context.

If you’re playing around with C# 8, and have been for a while, you may spot this message when using nullable reference types:

Warning CS8632 The annotation for nullable reference types should only be used in code within a ‘#nullable’ context.

There’s a couple of things you can check to fix this; the first is to check that you are, in fact, using C# 8. It’s likely that you are, because there is a separate error if you are not, but just for my own benefit:

<LangVersion>preview</LangVersion>

The next, and more likely step, is that you’re using the previous declaration for Nullable Reference Types; it used to look like this:

<NullableReferenceTypes>true</NullableReferenceTypes>

But no more! Now it looks like this:

<NullableContextOptions>enable</NullableContextOptions>

And, as if by magic – the error disappears!

But why? Well, it looks like they are making the setting more nuanced than simply on or off.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.