Skip to content

[Installation] Traditional .csproj format

Manuel Römer edited this page Oct 1, 2019 · 3 revisions

This guide shows you how to use the Nullable package with a traditional .NET Framework application which uses the old .csproj format by setting up a small demo application. If your project uses the new SDK .csproj style you can simply follow the .NET Standard/Core installation path.

Prerequisites

This guide requires an installation of Visual Studio 2019 which supports Nullable Reference Types. This can easily be done by updating to the latest version and ensuring that the .NET Core 3.0 SDK is installed.

1. Create the project

Create a new Console Application targeting the traditional .NET Framework. The target framework version doesn't matter as long as you choose a version >= 2.0.

2. Ensure that you can install NuGet packages via package references

⚠️ Important:
At the moment, the Nullable package is not compatible with the classig packages.config file (see this issue for an explanation). This can easily be circumvented by using Package References instead of the packages.config file though.

To ensure that you can install packages as package references, go to Visual Studio Options > NuGet Package Manager > General and ensure that the Allow format selection on first package install checkbox is checked. This allows you to choose if you want to use a packages.config file or package references when you install your first package.

Package References CheckBox

3. Install the package

Open the NuGet package manager by right clicking References > Manage NuGet packages.... Search for "Nullable" and install the package. In the dialog which pops up, choose the Package reference in project file option.

Package Install Options

4. Change the language version to C# 8.0

Now that the package has been installed, it's time to enable C# 8.0 so that the compiler supports Nullable Reference Types. You can do this by unloading your project, editing the XML of the .csproj and then reloading it.

First of all, unload your project by right clicking it and clicking Unload Project.

Unload Project

Then right click the project again and click Edit {YourProject}.csproj.

Edit Project

Afterwards, add the following XML code at the top of the file that got opened:

<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable> <!-- Note: At the time of writing this, this has no effect. It still doesn't hurt though. -->

It should now look somewhat similar to this:

csproj XML

Finally, right click the project again and click Reload Project.

Reload Project

5. Try out the attributes

Congratulations - you have done it! You should now be able to use the Nullable Attributes in the project. Go ahead and try it out by replacing Program.cs with this code:

#nullable enable // Important!

namespace NullableDemo
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    public class Program
    {
        [AllowNull]
        public static string AllowsNull { get; set; } = "Hello";
        public static string DoesntAllowNull { get; set; } = "Hello";

        public static void Main()
        {
            AllowsNull = null;
            DoesntAllowNull = null; // Should warn.
        }
    }
}

#nullable restore

If you have done everything correctly, you can now build your project and use these attributes. Furthermore, the code above should produce a compiler warning at the line with the comment.

To ensure that the NullableAttributes.cs file really got added to your project, feel free to put your cursor on [AllowNull] in the code and press F12 afterwards. The file should now be opened and you can see the code that will be compiled together with your project.

ℹ️ Note:
Be aware that projects using the old .csproj format require an explicit #nullable enable declaration in every file! This is a limitation of the C# compiler/project system and is not the fault of the Nullable package.

Clone this wiki locally