C
C#16mo ago
ingovals

❔ What could cause the compiler to not infer this nullability correctly

if (pageNumber.HasValue)
{
query["PageNumber"] = pageNumber.ToString();
}
if (pageNumber.HasValue)
{
query["PageNumber"] = pageNumber.ToString();
}
I get the warning here about possible null reference, but it is pretty obvious that this could be inferred by flow analysis. I'm assuming something is wrong, old codebase, old compile etc. I just don's use .net as much anymore and I haven't kept up. What should I look at?
6 Replies
Anton
Anton16mo ago
Anton
Anton16mo ago
see the question mark in the return type? that means it is allowed to return null look at nullable reference types, which are now enabled by default in new projects ah yeah also you should do pageNumber.Value.ToString
ingovals
ingovals16mo ago
I'm assuming that the pageNumber.Value could have been the issue, but I had to refactor my code anyways so I can't check
Angius
Angius16mo ago
if (pageNumber is {} pn)
{
query["PageNumber"] = pn.ToString();
}
if (pageNumber is {} pn)
{
query["PageNumber"] = pn.ToString();
}
this solves your issue btw
Akseli
Akseli16mo ago
query["PageNumber"] = pageNumber.ToString() ?? defaultValue;
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts