关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 bool是用于声明可以存储布尔值true或false的变量的关键字。它是System.Boolean的别名。
布尔关键字在内存中占用1个字节(8位)。 bool只有两个可能的值,即true或false 。
句法:
bool variable_name = value;
例子:
Input: true
Output: answer: False
Size of a byte variable: 1
Input: false
Output: Type of answer: System.Boolean
answer: True
Size of a bool variable: 1
范例1:
// C# program for bool keyword
using System;
using System.Text;
class GFG
{
static void Main(string[] args)
{
// bool variable declaration
bool answer = false;
// to print value
Console.WriteLine("answer: " + answer);
// to print size of a bool
Console.WriteLine("Size of a bool variable: " + sizeof(bool));
}
}
输出:
answer: False
Size of a bool variable: 1
范例2:
// C# program for bool keyword
using System;
using System.Text;
namespace geeks {
class GFG {
static void Main(string[] args)
{
// bool variable declaration
bool answer = true;
// to print type of variable
Console.WriteLine("Type of answer: " + answer.GetType());
// to print value
Console.WriteLine("answer: " + answer);
// to print size of a bool
Console.WriteLine("Size of a bool variable: " + sizeof(bool));
// hit ENTER to exit
Console.ReadLine();
}
}
}
输出:
Type of answer: System.Boolean
answer: True
Size of a bool variable: 1