I’m trying to start using null state analysis in our application: https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references
I’ve run in to the following issue: (with a new C# console app)
public interface ITester
{
string GetString();
}
public class Test(ITester t)
{
public void TestMethod()
{
var x = t.GetString();
}
}
When I hover over “var x” it is shown as being of type string?
whereas I want the interace to enforce nullability, so that the return object of GetString
really is string
, not string?
.
This is the same behaviour for other returned types too, the example here is just to keep it simple.
I expected the compiler to know the object returned via the interface cannot be null (because I have <Nullable>enable</Nullable>
set), but it doesn’t seem to.
What would I need to change here for it to realise that the “var x” variable is string
instead of string?
:
1