C
C#14mo 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
RobinTTY14mo ago
Here maybe a better screenshot, I wouldn't have expected the "possible null reference" here.
RobinTTY
RobinTTY14mo ago
NullReference
NullReference14mo ago
by itself, the bool variable cannot accept null, but since it is there? the variable can accept null
RobinTTY
RobinTTY14mo ago
Ok should have thought longer. ToString always returns a string?.
ero
ero14mo ago
why does the bool need to be nullable?
NullReference
NullReference14mo ago
if variable was null, ToString return null
ero
ero14mo ago
that is incorrect
RobinTTY
RobinTTY14mo 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
ero14mo ago
sure, question remains
RobinTTY
RobinTTY14mo ago
what question? 😄
ero
ero14mo ago
.
NullReference
NullReference14mo ago
?
MODiX
MODiX14mo 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
RobinTTY14mo ago
I don't understand the question in that case
ero
ero14mo ago
this is news to me
NullReference
NullReference14mo ago
for me too
MODiX
MODiX14mo 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
ero14mo ago
yeah, that have a method overload that does not take a bool
NullReference
NullReference14mo ago
how do you do that?
ero
ero14mo ago
async Task MyApiCaller() { }
async Task MyApiCaller(bool b) { }
async Task MyApiCaller() { }
async Task MyApiCaller(bool b) { }
!e
RobinTTY
RobinTTY14mo 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
ero14mo 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
RobinTTY14mo ago
yes right that's why the default is null
ero
ero14mo ago
what a cumbersome way to do this
RobinTTY
RobinTTY14mo ago
it has the same outcome as the overload lol
ero
ero14mo ago
shrug, i think it's ugly to work with nullable value types
RobinTTY
RobinTTY14mo ago
Sure that's taste
NullReference
NullReference14mo ago
I like what we're talking about and our avatars But a variable of type bool with a null value is strange.
RobinTTY
RobinTTY14mo 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
NullReference14mo ago
no one understands programming well enough. God is rated 5, everyone else is rated 3 xd
cumslvt13
cumslvt1314mo ago
in your case you should use HasValue instead of is not null
RobinTTY
RobinTTY14mo ago
Yeah I tried that, has the same outcome but from a cosmetic point sure
cumslvt13
cumslvt1314mo ago
still warning that variable can be null? Can you show your method signature?
RobinTTY
RobinTTY14mo ago
RobinTTY
RobinTTY14mo ago
vs
RobinTTY
RobinTTY14mo ago
RobinTTY
RobinTTY14mo ago
I'm too bad at googling to actually get to the article explaining why nullable works this way 😄
cumslvt13
cumslvt1314mo ago
I would say that that's visual studio issue
RobinTTY
RobinTTY14mo ago
Well visual studio displays a compiler warning
cumslvt13
cumslvt1314mo ago
no warnings for me in rider
RobinTTY
RobinTTY14mo ago
interesting
cumslvt13
cumslvt1314mo ago
also, run dotnet build
RobinTTY
RobinTTY14mo ago
but is b.ToString of type string? Console.WriteLine can write null so no warning
cumslvt13
cumslvt1314mo 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
cumslvt1314mo 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
RobinTTY14mo ago
oh yeah that's it I get it now wow
cumslvt13
cumslvt1314mo ago
even debugger shows that's null BUT if I step inside the .ToString() call
cumslvt13
cumslvt1314mo ago
cumslvt13
cumslvt1314mo ago
I'm inside Nullable.cs
RobinTTY
RobinTTY14mo ago
Yeah you are right, thanks 🙂
cumslvt13
cumslvt1314mo 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_kun14mo ago
I usually always use the .Value of a Nullable<T>
Accord
Accord14mo 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