0

I've encountered a few times now where I have nullable enabled and a property or field marked as a nullable interface type (with an appended ?), then if I go to use the property or field without a null check, I receive no error. (I would expect CS8602 - Dereference of a possibly null reference., and in concrete type properties and fields I do receive this error).

I am not sure if this is a Roslyn bug or something I don't adequately understand.

Additional information:

  • .NET 5
  • C# 9

Sample code


namespace dotnet_scratch {

    interface IFoo {
        string? name { get; set; }

        void fooAction ();
    }

    interface IFooGeneric<T> {
        string? name { get; set; }
        void fooAction( );
    }

    public class FooClass {
        IFoo? fooInterface;
        IFooGeneric<string>? fooInterfaceString;

        FooClass? fooClass;

        void doStuff( ) {
            System.Console.WriteLine( fooInterface.name );
            System.Console.WriteLine( fooInterfaceString.name );
            fooInterface.fooAction();
            fooInterfaceString.fooAction();
            fooClass.fooAction();
        }
        void fooAction () {}
    }
}

In the capture, the three highlighted errors are labeled:

(field) IFooGeneric<string>? FooClass.fooInterfaceString
'fooInterfaceString' may be null here.

Dereference of a possibly null reference. [dotnet_scratch]csharp(CS8602)

Additionally mousing over fooInterface.fooAction() and fooInterfaceString.fooAction() say: 'fooInterfaceString' is not null here.

My question is why is a property or field marked as IInterfaceType? not flagged as being potentially Nullable as is the case with the concrete typed properties and fields?

4
  • 3
    Please post the code as text in the question. That will make it a lot easier to read than squinting at a screenshot. You can use comments to indicate where there are warnings.
    – Jon Skeet
    Jan 2, 2021 at 18:44
  • 2
    But basically - by the time you're calling the methods, you've dereferenced the variable already to access name... so if that hasn't failed, the value is definitely not null. (Your question is pretty unclear given that you do have warnings for the interface type, in the first two lines of the method...)
    – Jon Skeet
    Jan 2, 2021 at 18:45
  • Hi Jon, thanks for the answer. I did add the text of the source to my original question now. But I do believe you have answered it. If I comment out the two .name references or I do fooInterface?.name and fooInterfaceString?.name the error is indeed shown on the two fooAction() calls. Thank you, I do appreciate it. If you create that as an answer I will gladly mark it as correct. Otherwise I can do "Answer your question".
    – ehiller
    Jan 6, 2021 at 13:41
  • 1
    I'll add an answer now.
    – Jon Skeet
    Jan 6, 2021 at 13:54

1 Answer 1

2

The compiler's view of whether the variable can have a null value or not has nothing to do with the type of the variable is a class or whether it's an interface - it's whether it's previously been dereferenced.

You received warnings for these two lines:

System.Console.WriteLine( fooInterface.name );
System.Console.WriteLine( fooInterfaceString.name );

... because that's the first time each of those variables has been dereferenced. But the compiler assumes that if both of those lines have executed without throwing an exception, then the later uses of the same variables:

fooInterface.fooAction();
fooInterfaceString.fooAction();

... will be fine. Neither variable can be null by this point in the execution flow, because otherwise one of the earlier lines would have thrown an exception. (The "nullability flow" doesn't take into account the possibility that another thread could change the value of the variable in the middle.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.