Instead of declaring:
bool one = false;
bool two = false;
bool three = false;
Is it possible to declare something like:
bool one, two, three;
while setting them all to false
?
bool one, two, three = false
Instead of declaring:
bool one = false;
bool two = false;
bool three = false;
Is it possible to declare something like:
bool one, two, three;
while setting them all to false
?
bool one, two, three = false
The default value of a bool variable is false. The default value of a bool? variable is null.
For reference you can visit: bool (C# Reference).
But you cannot use it unless assigned
with values.
bool one, two, three;
one = two = three = false
The same goes with nullable boolean:
bool? isCase = null;
if (isCase.HasValue){
//TODO:
}
You could either do:
bool one = false, two = false, three = false
Or
bool one, two, three;
one = two = three = false;
bools are false by default.
so
bool one, two, three;
Gives you three bools set to false. BUT - when you try to use them you'll get an error, eg:
Use of unassigned local variable 'three'
You need to initialise them before you use them:
bool one = false, two = false,three = false;
There is no built-in syntax to do that. And though bool
s have default value of false
, C# requires you to initialze variables before you use them.
The only way I cant think of that may help you is to declare an array:
bool[] bools = new bool[3];
Console.WriteLine(bools[0]);
Console.WriteLine(bools[1]);
Console.WriteLine(bools[2]);
The array is initialized with false
values, but you loose the semantics of your variable names (so I actually prefer Ashkan's answer).
2 Shorter ways to do that would be:
bool one = false, two = false, three = false;
Or:
bool one, two, three;
one = two = three = false;
If they are too many put them in an array and use loop. Otherwise, I think this works fine.
bool one, two, three;
one = two = three = false;