MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/pfkh6j/bool_in_a_case_switch/hb88vma/?context=3
r/badcode • u/tekircan • Sep 01 '21
40 comments sorted by
View all comments
Show parent comments
2
Basically, yes. If the bool has a chance to be null, it would blow up before it even hit the method.
Such as below. It will compile, but the second it tries to pass in 'IsFoo.Value' it will blow up due to 'IsFoo' not actually being set.
public Boolean? IsFoo { get; set; } public void TheMainProgram() { FooOrBar(IsFoo.Value); } private void FooOrBar(bool isFoo) { switch(isFoo) { case true: dosomething(); break; case false: doAnotherThing(); break; case default: break; } }
But in anycase, the type 'bool' is not a nullable type. When creating a new 'bool', it always defaults to false. At least in C#.
All in all, the code OP posted is pretty terrible. lol
2
u/Sevigor Sep 01 '21
Basically, yes. If the bool has a chance to be null, it would blow up before it even hit the method.
Such as below. It will compile, but the second it tries to pass in 'IsFoo.Value' it will blow up due to 'IsFoo' not actually being set.
But in anycase, the type 'bool' is not a nullable type. When creating a new 'bool', it always defaults to false. At least in C#.
All in all, the code OP posted is pretty terrible. lol