How to Enable Nullable Reference Types Feature of C# 8.0 for the Whole Project

How to enable Nullable Reference Types feature of C# 8.0 for the whole project

In Visual Studio 16.2 (from preview 1) the property name is changed to Nullable, which is simpler and aligns with the command line argument.

Add the following properties to your .csproj file.

<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

If you're targeting netcoreapp3.0 or later, you don't need to specify a LangVersion to enable nullable reference types.


Alternatively, if you like using GUIs, open the Project Properties UI, search for nullable and select the option you want:

Sample Image


For older Visual Studio versions:

  • From 16.0 preview 2 to 16.1, set NullableContextOptions to enable.
  • In 16.0 preview 1, set NullableReferenceTypes to true.

How to make reference type nullable in C# 7.3?

The compiler is asking you to use a newer version of the language, not a newer version of .NET. Every version of .NET supports nullable references types. References could be null in any version of .NET, you just were not able to annotate that in C# until C# 8.0.

However, your project is configured for C# 7.3, you need to change the language version. Please refer to How to enable Nullable Reference Types feature of C# 8.0 for the whole project.

See also What is the difference between C# and .NET?

Once you have your project configured for C# 8.0 and with Nullable Reference Types enabled… Roslyn, the C# compiler, will understand nullability annotations and provide code analysis based on them.

And you will have to deal with null anyway. Which might mean null checks. Accessing a member of a null reference is still an NullReferenceException, even in C# 8.0. At least the static analysis will help you.

Refer to What is a NullReferenceException, and how do I fix it?.


On the odd chance that you actually need to run newer features on an old runtime (e.g. async/await in .NET 2.0), I might have a solution for you: Theraot.Core. Of which, full disclosure, I'm the author.

Customizing .csproj in Unity enable nullable reference types

One way is to go to File->Build Settings..->Player Settings->Player, scroll down to 'Additional Compiler Arguments' and add -nullable+.

How to use type reference nullable or how to enable it on preview in .NET Core

A string is Always nullable in C#.

And while C# 8 will introduce the nullable reference types like public string? this is also a problem NewtonSoft's JSON Converter has had solved for some time:

string ignored = JsonConvert.SerializeObject(movie,
Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });


Edit

I made it back to the machine with Visual Studio, fully updated to Visual Studio 2019 (16.2):

If you edit the project solution

  1. Open the solution folder
  2. Right-click the solution and edit

and add the following two settings to the PropertyGroup the warning will go away:

<Nullable>enable</Nullable>
<LangVersion>8.0</LangVersion>


Related Topics



Leave a reply



Submit