C
C#2y ago
RobinTTY

❔ Nullable bools

I just got into the situation where I needed to use nullable bools and I'm a little confused about how the compiler handles things. If I write my code like shown in picture 1 the compiler still thinks the variable accessScopesSupported might be null inside the codeblock with an explicit check for null. If I use .Value inside the codeblock the compiler understands that the value cannot be null. My question is why? Shouldn't the null check be enough?
54 Replies
RobinTTY
RobinTTYOP2y ago
Here maybe a better screenshot, I wouldn't have expected the "possible null reference" here.
RobinTTY
RobinTTYOP2y ago
NullReference
NullReference2y ago
by itself, the bool variable cannot accept null, but since it is there? the variable can accept null
RobinTTY
RobinTTYOP2y ago
Ok should have thought longer. ToString always returns a string?.
ero
ero2y ago
why does the bool need to be nullable?
NullReference
NullReference2y ago
if variable was null, ToString return null
ero
ero2y ago
that is incorrect
RobinTTY
RobinTTYOP2y ago
it is a case where if something isn't sent to an API it will return a different result from when a "false" or "true" is sent. All 3 have different outcomes
ero
ero2y ago
sure, question remains
RobinTTY
RobinTTYOP2y ago
what question? 😄
ero
ero2y ago
.
NullReference
NullReference2y ago
?
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
int? i = null;
i.ToString()
int? i = null;
i.ToString()
Result: string




Compile: 531.812ms | Execution: 28.921ms | React with ❌ to remove this embed.
RobinTTY
RobinTTYOP2y ago
I don't understand the question in that case
ero
ero2y ago
this is news to me
NullReference
NullReference2y ago
for me too
MODiX
MODiX2y ago
Ero#1111
REPL Result: Failure
List<string>? foo = null;
foo.ToString()
List<string>? foo = null;
foo.ToString()
Exception: NullReferenceException
- Object reference not set to an instance of an object.
- Object reference not set to an instance of an object.
Compile: 497.655ms | Execution: 35.858ms | React with ❌ to remove this embed.
ero
ero2y ago
yeah, that have a method overload that does not take a bool
NullReference
NullReference2y ago
how do you do that?
ero
ero2y ago
async Task MyApiCaller() { }
async Task MyApiCaller(bool b) { }
async Task MyApiCaller() { }
async Task MyApiCaller(bool b) { }
!e
RobinTTY
RobinTTYOP2y ago
It seems natural to me, if you don't want something to be in the query string to the API you specify it as null. If you want it to be there you either choose true or false. But this is of course arguable and my default is just null so calling the method is not any harder
ero
ero2y ago
what lol if you don't want something in a query, you don't specify it at all not specify it as null null is distinctly different from nothing
RobinTTY
RobinTTYOP2y ago
yes right that's why the default is null
ero
ero2y ago
what a cumbersome way to do this
RobinTTY
RobinTTYOP2y ago
it has the same outcome as the overload lol
ero
ero2y ago
shrug, i think it's ugly to work with nullable value types
RobinTTY
RobinTTYOP2y ago
Sure that's taste
NullReference
NullReference2y ago
I like what we're talking about and our avatars But a variable of type bool with a null value is strange.
RobinTTY
RobinTTYOP2y ago
Haha, yeah first time I needed to use it as well. But it seemed very much made for this kind of problem to me. But maybe I'm completely off on this one It was already weird to me that the API accepts a boolean query string but returns different data for 3 cases (query string not sent, true sent, false sent)
NullReference
NullReference2y ago
no one understands programming well enough. God is rated 5, everyone else is rated 3 xd
cumslvt13
cumslvt132y ago
in your case you should use HasValue instead of is not null
RobinTTY
RobinTTYOP2y ago
Yeah I tried that, has the same outcome but from a cosmetic point sure
cumslvt13
cumslvt132y ago
still warning that variable can be null? Can you show your method signature?
RobinTTY
RobinTTYOP2y ago
RobinTTY
RobinTTYOP2y ago
vs
RobinTTY
RobinTTYOP2y ago
RobinTTY
RobinTTYOP2y ago
I'm too bad at googling to actually get to the article explaining why nullable works this way 😄
cumslvt13
cumslvt132y ago
I would say that that's visual studio issue
RobinTTY
RobinTTYOP2y ago
Well visual studio displays a compiler warning
cumslvt13
cumslvt132y ago
no warnings for me in rider
RobinTTY
RobinTTYOP2y ago
interesting
cumslvt13
cumslvt132y ago
also, run dotnet build
RobinTTY
RobinTTYOP2y ago
but is b.ToString of type string? Console.WriteLine can write null so no warning
cumslvt13
cumslvt132y ago
important thing here why this works is difference between nullable reference and value types
int? i = null;
i.ToString()
int? i = null;
i.ToString()
cumslvt13
cumslvt132y ago
Whenever you add ? to VALUE type it's just a syntax sugar not to write Nullable<T> so int? despite being a null is in fact a nullable object and... it's not null
RobinTTY
RobinTTYOP2y ago
oh yeah that's it I get it now wow
cumslvt13
cumslvt132y ago
even debugger shows that's null BUT if I step inside the .ToString() call
cumslvt13
cumslvt132y ago
cumslvt13
cumslvt132y ago
I'm inside Nullable.cs
RobinTTY
RobinTTYOP2y ago
Yeah you are right, thanks 🙂
cumslvt13
cumslvt132y ago
but the story is different with REFERENCE types, ? for them does (almost) nothing @Ero @NullReference Might be interesting for you, since you were surprised by the int? behavior
kunio_kun
kunio_kun2y ago
I usually always use the .Value of a Nullable<T>
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