Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell C# nullable references that a method is effectively a null check on a field

Consider the following code:

#nullable enable
class Foo
{
    public string? Name { get; set; }
    public bool HasName => Name != null;
    public void NameToUpperCase()
    {
        if (HasName)
        {
            Name = Name.ToUpper();
        }
    }
}

On the Name=Name.ToUpper() I get a warning that Name is a possible null reference, which is clearly incorrect. I can cure this warning by inlining HasName so the condition is if (Name != null).

Is there any way I can instruct the compiler that a true response from HasName implies a non-nullability constraint on Name?

This is important because HasName might actually test a lot more things, and I might want to use it in several places, or it might be a public part of the API surface. There are many reasons to want to factor the null check into it's own method, but doing so seems to break the nullable reference checker.

like image 747
John Melville Avatar asked Nov 24 '19 14:11

John Melville


2 Answers

UPDATE:

C# 9.0 introduced what you're looking for in the form of MemberNotNullWhenAttribute. In your case you want:

#nullable enable
class Foo
{
    public string? Name { get; set; }

    [MemberNotNullWhen(true, nameof(Name))]
    public bool HasName => Name != null;
  
    public void NameToUpperCase()
    {
        if (HasName)
        {
            Name = Name.ToUpper();
        }
    }
}

There's also MemberNotNullAttribute for unconditional assertions.

Old answer:

I looked around at the different attributes from System.Diagnostics.CodeAnalysis and I couldn't find anything applicable, which is very disappointing. The closest you can get to what you want appears to be:

public bool TryGetName([NotNullWhen(true)] out string? name)
{
    name = Name;
    return name != null;
}

public void NameToUpperCase()
{
    if (TryGetName(out var name))
    {
        Name = name.ToUpper();
    }
}

It looks pretty cumbersome, I know. You can look at the MSDN docs for nullable attributes, maybe you'll find something neater.

like image 169
V0ldek Avatar answered Oct 25 '22 10:10

V0ldek


In C# 9.0 check out [MemberNotNull(nameof(Property))] and [MemberNotNullWhen(true, nameof(Property))] attributes.

https://github.com/dotnet/runtime/issues/31877

like image 39
John Melville Avatar answered Oct 25 '22 11:10

John Melville