C
C#2y 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
Anton2y ago
Anton
Anton2y 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
ingovalsOP2y 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
Angius2y ago
if (pageNumber is {} pn)
{
query["PageNumber"] = pn.ToString();
}
if (pageNumber is {} pn)
{
query["PageNumber"] = pn.ToString();
}
this solves your issue btw
Akseli
Akseli2y ago
query["PageNumber"] = pageNumber.ToString() ?? defaultValue;
Accord
Accord2y 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