This question already has answers here:
C#: Assign same value to multiple variables in single statement
                                
                                    (10个答案)
                                
                        
                                2年前关闭。
            
                    
而不是声明:

bool one = false;
bool two = false;
bool three = false;


是否可以声明如下内容:

bool one, two, three;


同时将它们全部设置为false吗?

bool one, two, three = false

最佳答案

bool变量的默认值为false。布尔值的默认值?变量为空。


作为参考,您可以访问:bool (C# Reference).

但是除非assigned带有值,否则不能使用它。

bool one, two, three;
one = two = three = false


可为空的布尔值也是如此:

bool? isCase = null;
if (isCase.HasValue){
    //TODO:
   }

10-08 07:06