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?