Why does this require a cast?
I have this method:
Even though I've null-checked
mini
, VS is still telling me that I need an explicit cast to bool
on mini ? Formatting.None : Formatting.Indented
Why?16 Replies
Because
mini
is of type bool?
You canot change a variable's type
You could try pattern matching like thisSo using
?
is kind of like an entirely new type?
In a sense?Yes
bool?
and bool
are two different typesI see
Makes sense
To be precise, since
bool
is a value type, bool?
gets lowered by the compiler to Nullable<bool>
Ok, that makes sense
Ty
Angius
REPL Result: Success
Console Output
Compile: 464.147ms | Execution: 52.290ms | React with ❌ to remove this embed.
You need
!e
to evaluate the codei hate this bot. not telling me how to use it
Angius
REPL Result: Success
Console Output
Compile: 465.248ms | Execution: 52.642ms | React with ❌ to remove this embed.
ok just wanted to point out :
null != true and null != false and true != false
the nullable bools has exactly 3 possible values, and if you compare against one, the others are false. and if you compare against "not one" there are 2 positive results (just negated).
whichever one it is
because
bool?
is not a bool
i read somewhere that behind the scenes, the compiler takes bool?
and outputs Nullable<bool>
as the type, accounting for any other type (so really its Nullable<T>
)
i may be wrong though, so take it with a grain of saltYou might have read it here lol
Angius
To be precise, since
bool
is a value type, bool?
gets lowered by the compiler to Nullable<bool>
Quoted by
<@85903769203642368> from #Why does this require a cast? (click here)
React with ❌ to remove this embed.
100% skipped all of that with the assumption it wasnt said already lol