bool(C# 参考)bool (C# Reference)

bool 关键字是 System.Boolean 的别名。The bool keyword is an alias of System.Boolean. 它用于声明变量来存储布尔值:truefalseIt is used to declare variables to store the Boolean values: true and false.

备注

如需支持三值逻辑(例如,在使用支持三值布尔类型的数据库时),请使用 bool? 类型。Use the bool? type, if you need to support the three-valued logic, for example, when you work with databases that support a three-valued Boolean type. 对于 bool? 操作数,预定义的 &| 运算符支持三值逻辑。For the bool? operands, the predefined & and | operators support the three-valued logic. 有关详细信息,请参阅布尔逻辑运算符一文的可以为 null 的布尔逻辑运算符部分。For more information, see the Nullable Boolean logical operators section of the Boolean logical operators article.

文本Literals

可将布尔值赋给 bool 变量。You can assign a Boolean value to a bool variable. 也可以将计算结果为 bool 类型的表达式赋给 bool 变量。You can also assign an expression that evaluates to bool to a bool variable.

public class BoolTest
{
    static void Main()
    {
        bool b = true;

        // WriteLine automatically converts the value of b to text.
        Console.WriteLine(b);

        int days = DateTime.Now.DayOfYear;


        // Assign the result of a boolean expression to b.
        b = (days % 2 == 0);

        // Branch depending on whether b is true or false.
        if (b)
        {
            Console.WriteLine("days is an even number");
        }
        else
        {
            Console.WriteLine("days is an odd number");
        }   
    }
}
/* Output:
  True
  days is an <even/odd> number
*/

bool 变量的默认值为 falseThe default value of a bool variable is false. bool? 变量的默认值为 nullThe default value of a bool? variable is null.

转换Conversions

在 C++ 中,bool 类型的值可转换为 int 类型的值;也就是说,false 等效于零值,而 true 等效于非零值。In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. 在 C# 中,不存在 bool 类型与其他类型之间的相互转换。In C#, there is no conversion between the bool type and other types. 例如,下面的 if 语句在 C# 中无效:For example, the following if statement is invalid in C#:

int x = 123;

// if (x)   // Error: "Cannot implicitly convert type 'int' to 'bool'"
{
    Console.Write("The value of x is nonzero.");
}

若要测试 int 类型的变量,必须将该变量与一个值(例如零)进行显式比较,如下所示:To test a variable of the type int, you have to explicitly compare it to a value, such as zero, as follows:


if (x != 0)   // The C# way
{
    Console.Write("The value of x is nonzero.");
}

示例Example

在此例中,你通过键盘输入一个字符,然后程序检查输入的字符是否是一个字母。In this example, you enter a character from the keyboard and the program checks if the input character is a letter. 如果字符是一个字母,则程序检查它是大写还是小写。If it is a letter, it checks if it is lowercase or uppercase. 执行这些检查使用的是 IsLetterIsLower,二者均返回 bool 类型:These checks are performed with the IsLetter, and IsLower, both of which return the bool type:

public class BoolKeyTest
{
    static void Main()
    {
        Console.Write("Enter a character: ");
        char c = (char)Console.Read();
        if (Char.IsLetter(c))
        {
            if (Char.IsLower(c))
            {
                Console.WriteLine("The character is lowercase.");
            }
            else
            {
                Console.WriteLine("The character is uppercase.");
            }
        }
        else
        {
            Console.WriteLine("Not an alphabetic character.");
        }
    }
}
/* Sample Output:
    Enter a character: X
    The character is uppercase.
 
    Enter a character: x
    The character is lowercase.

    Enter a character: 2
    The character is not an alphabetic character.
 */

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also