Why does this require a cast?

I have this method:
c#
public string JsonSerialize(bool? mini) {
mini = mini == null ? true : mini;
JsonSettings.Formatting = mini ? Formatting.None : Formatting.Indented;
return JsonConvert.SerializeObject(this, JsonSettings);
}
c#
public string JsonSerialize(bool? mini) {
mini = mini == null ? true : mini;
JsonSettings.Formatting = mini ? Formatting.None : Formatting.Indented;
return JsonConvert.SerializeObject(this, JsonSettings);
}
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
Angius
Angius10mo ago
Because mini is of type bool? You canot change a variable's type
public string JsonSerialize(bool? mini)
{
JsonSettings.Formatting = mini is not false ? Formatting.None : Formatting.Indented;
return JsonConvert.SerializeObject(this, JsonSettings);
}
public string JsonSerialize(bool? mini)
{
JsonSettings.Formatting = mini is not false ? Formatting.None : Formatting.Indented;
return JsonConvert.SerializeObject(this, JsonSettings);
}
You could try pattern matching like this
UnemployedNinja
UnemployedNinjaOP10mo ago
So using ? is kind of like an entirely new type? In a sense?
Angius
Angius10mo ago
Yes bool? and bool are two different types
UnemployedNinja
UnemployedNinjaOP10mo ago
I see Makes sense
Angius
Angius10mo ago
To be precise, since bool is a value type, bool? gets lowered by the compiler to Nullable<bool>
UnemployedNinja
UnemployedNinjaOP10mo ago
Ok, that makes sense Ty
MODiX
MODiX10mo ago
Angius
REPL Result: Success
foreach (var v in new bool?[] { null, true, false }) { var check = v is not false; Console.WriteLine($"For v being {(v is null ? "null" : v.ToString())}, the statement 'v is not false' is {check}") ;
}
foreach (var v in new bool?[] { null, true, false }) { var check = v is not false; Console.WriteLine($"For v being {(v is null ? "null" : v.ToString())}, the statement 'v is not false' is {check}") ;
}
Console Output
For v being null, the statement 'v is not false' is True
For v being True, the statement 'v is not false' is True
For v being False, the statement 'v is not false' is False
For v being null, the statement 'v is not false' is True
For v being True, the statement 'v is not false' is True
For v being False, the statement 'v is not false' is False
Compile: 464.147ms | Execution: 52.290ms | React with ❌ to remove this embed.
DΣX
DΣX10mo ago
foreach (var v in new bool?[] { null, true, false }) { var check = v is true;
Console.WriteLine($"For v being {(v is null ? "null" : v.ToString())}, the statement 'v is not false' is {check}") ;
}
foreach (var v in new bool?[] { null, true, false }) { var check = v is true;
Console.WriteLine($"For v being {(v is null ? "null" : v.ToString())}, the statement 'v is not false' is {check}") ;
}
Angius
Angius10mo ago
You need !e to evaluate the code
DΣX
DΣX10mo ago
i hate this bot. not telling me how to use it
MODiX
MODiX10mo ago
Angius
REPL Result: Success
foreach (var v in new bool?[] { null, true, false }) { var check = v is true;
Console.WriteLine($"For v being {(v is null ? "null" : v.ToString())}, the statement 'v is true' is {check}") ;
}
foreach (var v in new bool?[] { null, true, false }) { var check = v is true;
Console.WriteLine($"For v being {(v is null ? "null" : v.ToString())}, the statement 'v is true' is {check}") ;
}
Console Output
For v being null, the statement 'v is true' is False
For v being True, the statement 'v is true' is True
For v being False, the statement 'v is true' is False
For v being null, the statement 'v is true' is False
For v being True, the statement 'v is true' is True
For v being False, the statement 'v is true' is False
Compile: 465.248ms | Execution: 52.642ms | React with ❌ to remove this embed.
DΣX
DΣX10mo ago
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
oke
oke10mo ago
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 salt
Angius
Angius10mo ago
You might have read it here lol
MODiX
MODiX10mo ago
Angius
To be precise, since bool is a value type, bool? gets lowered by the compiler to Nullable<bool>
Quoted by
React with ❌ to remove this embed.
oke
oke10mo ago
100% skipped all of that with the assumption it wasnt said already lol
Want results from more Discord servers?
Add your server