Overlord Zurg
Overlord Zurg

Reputation: 3794

C# 10 - is there a way to make "var" define the variable as non-nullable?

When I write this statement:

var x = new ClassName();

x is implicitly typed as a ClassName?. That is, since ClassName is a reference type, implicit assignments using var automatically define as nullable (even when I am providing a non-null instance and never modifying it).

My question is, is there any way to make non-nullability the default when using the "var" keyword?

I'm aware of this question and associated info: Why does Visual Studio Type a Newly Minted Array as Nullable?

Upvotes: 10

Views: 1837

Answers (2)

The Lemon
The Lemon

Reputation: 1389

(answering the use case in your comment more so than the question). If you want a readonly non-nullable decorator (presumably at the method level scope, otherwise you could just use the 'readonly' declaration), you could create a generic class with the functionality you're after. It's not quite an alternative to 'var' - but it would cater to your use case to some degree.

public class ReadOnly<T>
{
    private T _value;
    public T value { get { return _value; } }
    public ReadOnly(T initialValue)
    {
        if (initialValue == null)
            throw new ArgumentException("value must be non-null");
        _value = initialValue;
    }
    public static implicit operator T(ReadOnly<T> readOnly)
    {
        return readOnly.value;
    }
}
public class Foo {
    public void Bar() {
        var myReadonlyFoo = new ReadOnly<Foo>(this);
        myReadonlyFoo.value = this;//compile error
        var tempVar = myReadonlyFoo.value;//this works fine
        myReadonlyFoo.value.Bar();//this works fine, but will crash the app lol

        Foo test = myReadonlyFoo; //with some implicit conversion for the fun of it

    }
}

Upvotes: -1

Guru Stron
Guru Stron

Reputation: 142173

is there any way to make non-nullability the default when using the "var" keyword

No, as it is stated in the docs - var always implies nullable reference type:

When var is used with nullable reference types enabled, it always implies a nullable reference type even if the expression type isn't nullable. The compiler's null state analysis protects against dereferencing a potential null value. If the variable is never assigned to an expression that maybe null, the compiler won't emit any warnings. If you assign the variable to an expression that might be null, you must test that it isn't null before dereferencing it to avoid any warnings.

But null state analysis can determine if variable is not actually null and do not emit warnings in quite a lot of cases (hence the part with emphasis in the quote):

public class C 
{
    public string M(string? j) {
        var foo = "";
        var bar = j;
        // return foo; // no warning
        return bar; // warning
    }
}

Demo

If for some reason you still need explicitly non-nullable type you can work around with target typed new expressions in some cases:

MyClass x = new();

Also you can consider disabling nullable reference types (locally of for the whole project) or using null-forgiving operator (!).

Upvotes: 9

Related Questions