Introduction

As we know there are so many changes in .Net 6 and C# 10. In this article, we will explore one of the changes in C#10 on the writing properties and objects of a class. More details on new features or changes in .NET 6 can be found in the previous article Features of .NET 6.

In .NET 5 and lower versions, we can write properties and objects of class like below.

public class Person
    {
        public string Name;      
        public string Sex;
        public string Address;
        public string Email;

    }

However, it has been changed in C#10. If we write like earlier, it shows warning like below.

So, to handle this there are different ways. In this article, we will learn those methods to handle the warnings.

Let’s create a console application in .NET6

Create Console Application in .NET 6

Step 1- Open Visual Studio 2022 and click Create a new project.

Step 2 – Select Console App and click Next.

Step 3 – Give the project name and location of the project.

Step 4 – Select framework: .NET 6.0 (Long-term support).

This creates the console app which looks like below.

Default Program.cs file is listed below.

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Now, let’s compile and run the program. when you run it, it will display the “Hello, World!” message. Now, we will move to the main point of this write-up.

Add a New Class

Right-click on project -> add class and give the name of the class as Person.

Person class contains the following properties. Write the below code.

public class Person
    {
        public string Name;      
        public string Sex;
        public string Address;
        public string Email;
        public string Description { get; set; }

    }

Then, you will get the warning as depicted below. The compiler assumes these properties are non-nullable.

Let’s jump to different ways to handle the compiler warning.

Method 1

Changing the project file(project.csproj). You can disable or remove the nullable in csproj file so that the compiler will not show the warning message. Right-click on the project and Edit the project as shown below.

 Then project file will be opened, and you can simply disable Nullable as illustrated below.

Inside the PropertyGroup change Nullable to disable as shown below or remove that line.

<Nullable>disable</Nullable>

Method 2: Giving default value

You can assign it in different ways:

  • a default value
  • reasonable default value as string.Empty or
  • “”

as illustrated in the below code.

public class Person
    {
        public string Name="Default Value";      
        public string Sex="";
        public string Address="";
        public string Email=string.Empty;
        public string Description { get; set; } = string.Empty;

    }

Method 3- Another way to handle it is to make properties nullable reference type by simply using “?” as demonstrated below.

public class Person
    {
        public string? Name; 
        public string? Sex ;
        public string? Address ;
        public string? Email;
        public string? Description { get; set; }

    }

Overall, in .NET 6 we should define the variable, properties, or field explicitly as either nullable or non-nullable with a reference type.

Summary

In this article, I have created a console application in .NET 6 and demonstrated issues with fields or properties if we code as like in .NET 5 or lower. Additionally, the article has provided the different methods to handle such situations in .NET 6. Hence, these are the three ways to handle the nullable, non-nullable field and properties in .NET 6. I hope you have got an idea about it.

10 thoughts on “How to Handle Nullable Reference in .NET 6”
  1. Wonderful post! We will be linking to this particularly great content on our site.
    Keep up the great writing.

  2. Hello! I could have sworn I’ve been to this blog before but after checking through
    some of the post I realized it’s new to me. Anyways, I’m definitely delighted I found it and I’ll be
    book-marking and checking back frequently!

  3. I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to create my own blog and
    would like to find out where u got this from. thanks

  4. Does your site have a contact page? I’m having a tough time locating it but, I’d like
    to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it develop over
    time.

  5. Hi, its nice article concerning media print, we all understand media is a fantastic source of facts.

  6. I’m really loving the theme/design of your website.
    Do you ever run into any web browser compatibility issues?

    A few of my blog visitors have complained about my website not operating
    correctly in Explorer but looks great in Opera.
    Do you have any recommendations to help fix this issue?

  7. Hi to every , since I am truly eager of reading this
    web site’s post to be updated daily. It contains fastidious material.

  8. I don’t even know how I ended up here, but I thought this post was good.
    I don’t know who you are but definitely
    you are going to a famous blogger if you aren’t already 😉 Cheers!

  9. Does your site have a contact page? I’m having problems locating it
    but, I’d like to shoot you an e-mail. I’ve got some creative ideas
    for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it
    develop over time.

Leave a Reply to Business Cancel reply

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