I am working in a project with nullable annotation context enabled, but we have a dependency on a library with nullable annotation context disabled.
Simplified example of the static analysis behavior I'm seeing in our project:
class Something
{
static void Main(string[] args)
{
Foo foo = new();
Console.WriteLine("foo.Bar: " + foo.Bar); // Null static analysis claim: 'Bar' is not null here. Runtime behavior: 'Bar' is null
}
}
#nullable disable
class Foo
{
public string Bar { get; set; }
}
My question is: is this static analysis behavior expected? It would be great to be able to rely on the null static analysis features to reason about our code & omit unnecessary null checks, but this is giving me doubts.
Yes, this is by design. When you have a code compiled with NRT disabled you will get nullable-oblivious state. From Nullable contexts section of the docs:
From the Nullable reference types (NRTs) docs:
And from Understand contexts and warnings: