Home > Enterprise >  C# Are Classes not nullable anymore?
C# Are Classes not nullable anymore?

Time:10-14

Using C# in .NET 6.0 I am coming across warnings which say "Cannot convert null literal to non-nullable reference type." Which I thought classes were nullable and were able to be set as null...

This produces the Warning:

    public class Foo
    {
        public string Name { get; set; }

        public Foo()
        {
            Name = "";
        }
    }

    public class Bar
    {
        public Foo fooClass;

        public Bar()
        {
            // Because I don't want to set it as anything yet.
            fooClass = null;
        }

        public void FooBarInit()
        {
            fooClass = new Foo();
        }
    }

But doing this gives me no warning

    public class Foo
    {
        public string Name { get; set; }

        public Foo()
        {
            Name = "";
        }
    }

    public class Bar
    {
        public Foo? fooClass;

        public Bar()
        {
            // Because I don't want to set it as anything yet.
            fooClass = null;
        }

        public void FooBarInit()
        {
            fooClass = new Foo();
        }
    }

However now lets attempt to use the Name variable in Foo inside of Bar

    public class Foo
    {
        public string Name { get; set; }

        public Foo()
        {
            Name = "";
        }
    }

    public class Bar
    {
        public Foo? fooClass;

        public Bar()
        {
            // Because I don't want to set it as anything yet.
            fooClass = null;
        }

        public void FooBarInit()
        {
            fooClass = new Foo();
        }

        public void FooBarTest()
        {
            Console.WriteLine(fooClass.Name); // Warning here which tells me fooClass maybe null
        }
    }

However FooBarTest will never run without the FooBarInit being ran first. So it'll never be null and if it is, I would have an error handling situation after that.

My question is, why do I have to set classes to allow null when they should inherently accept null anyway?

If I use the "?" after declaring a class... I now have to check if it's null... ANY time I want to call that class, it makes my code look atrocious. Any fixes or ability to turn it off?

CodePudding user response:

Although it's a very nice feature, you can still disable it by changing the value of the Nullable property to disable in your .csproj file:

  <PropertyGroup>
    ...
    <Nullable>disable</Nullable>
    ...
  </PropertyGroup>

CodePudding user response:

If you know that the value of a nullable type cannot possibly be null, you can use the null-forgiving operator.

Console.WriteLine(fooClass!.Name);

You can also (bizarrely to this Kotlin dev) assign null to a non-nullable type using this mechanism:

public Foo fooClass = null!;

This answer suggests it's similar to using lateinit in Kotlin, which says to the compiler "I promise this will have been set to something non-null by the time I come to use it". You can initialize the variable to null with this technique, but you can't set it to null later on.

  • Related