There are a number of similar questions on SO, and looking at those it always seems the asker has missed a path. But this one has got me stumped. In its simplest form:
public Object Test() {
string testStr = "test";
if (testStr != null) {
return null;
}
}
It’s fine if the string is a const
public Object Test() {
const string testStr = "test";
if (testStr != null) {
return null;
}
}
Why does the compiler suggest that the value of testStr
may be null (the value may change between the explicit setting and the condition check)?
(VS2022, .NET8, Nullable
disabled)