18
votes

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.

3
IMO you should use HasValue on a nullable type, not check it against null. It probably doesn't affect your problem though. - fredrik
I think for you case, you can wrap you code with #nullable disable then #nullable enable or restore again afterwards (docs.microsoft.com/en-us/dotnet/csharp/…). - GaryNg
you could use the "dammit" ! operator. if(HasName) { Name = Name!.ToUpper(); } - Jan Paolo Go
for a multi-thread application, you could have Name being null after the HasName check, using the variable locally instead of going back to the property (who knows what the property might do in its getter) is going to give some funky bugs (remember the using of an event handler where this has happened alot) - XIU

3 Answers

7
votes

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.

1
votes

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

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

-10
votes

String is a reference type, and nullable (e.g. int?) is nullable value types. So you can't really do this string? myString; What you need is this:

class Foo
{
    public string Name { get; set; }
    public bool HasName => !String.IsNullOrEmpty(Name);  ////assume you want empty to be treated same way as null
    public void NameToUpperCase()
    {
        if (HasName)
        {
            Name = Name.ToUpper();
        }
    }
}