❔ 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
Here maybe a better screenshot, I wouldn't have expected the "possible null reference" here.
by itself, the bool variable cannot accept null, but since it is there? the variable can accept null
Ok should have thought longer. ToString always returns a
string?
.why does the bool need to be nullable?
if variable was null, ToString return null
that is incorrect
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
sure, question remains
what question? 😄
.
?
Ero#1111
REPL Result: Success
Result: string
Compile: 531.812ms | Execution: 28.921ms | React with ❌ to remove this embed.
I don't understand the question in that case
this is news to me
for me too
Ero#1111
REPL Result: Failure
Exception: NullReferenceException
Compile: 497.655ms | Execution: 35.858ms | React with ❌ to remove this embed.
yeah, that
have a method overload that does not take a bool
how do you do that?
!e
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
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
yes right that's why the default is null
what a cumbersome way to do this
it has the same outcome as the overload
lol
shrug, i think it's ugly to work with nullable value types
Sure that's taste
I like what we're talking about and our avatars
But a variable of type bool with a null value is strange.
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)
no one understands programming well enough. God is rated 5, everyone else is rated 3
xd
in your case you should use
HasValue
instead of is not null
Yeah I tried that, has the same outcome but from a cosmetic point sure
still warning that variable can be null? Can you show your method signature?
vs
I'm too bad at googling to actually get to the article explaining why nullable works this way 😄
I would say that that's visual studio issue
Well visual studio displays a compiler warning
no warnings for me in rider
interesting
also, run
dotnet build
but is b.ToString of type string?
Console.WriteLine can write null so no warning
important thing here why this works is difference between nullable reference and value types
this translates to:
((Nullable<int>)null).ToString();
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYwz6HGUCWAdgC4D86V6AvOjQK4A23A3BUpEqAOgAqAewDKdAE60A5gAoAlAKSUAvsk1A===SharpLab
C#/VB/F# compiler playground.
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 nulloh yeah that's it
I get it now
wow
even debugger shows that's null BUT if I step inside the
.ToString()
callI'm inside
Nullable.cs
Yeah you are right, thanks 🙂
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?
behaviorI usually always use the .Value of a Nullable<T>
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.