ylbtech-Docs-.NET-C # - Guide - Language Reference - keyword - value types: bool |
1. Back to top |
bool (C # Reference)
bool
Keywords are System.Boolean alias. It is used to declare variables to store Boolean values: to true and false .
Remark
To support the three-valued logic (for example, when using three-valued Boolean database support), use bool?
type. For bool?
operands predefined &
and |
operator supports three-valued logic. For more information, see the Boolean operator a text may be a Boolean operator is null section.
text
Boolean values can be assigned to bool
variables. The results can also be as a bool
type of expression assigned to bool
a 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
The default value of variables false
. bool?
The default value of variables null
.
Change
In C ++, bool
the value type can be converted to int
value type; that is, false
equivalent to a zero value, and true
is equivalent to a non-zero value. In C #, there is no bool
conversion between types of other types. 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."); }
To test int
the type of a variable, the variable must be a value (e.g., zero) an explicit comparison, as follows:
if (x != 0) // The C# way { Console.Write("The value of x is nonzero."); }
Examples
In this example, you enter a character from the keyboard, and then enter the program checks whether a character is a letter. If the character is a letter, then it checks its case. These checks are performed using IsLetter and islower , both return bool
types:
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 # Language Specification
For more information, see the C # language specification . The specification language is C # syntax and usage of authoritative information.
See
2. Return to top |
3. Back to top |
4. Top |
5. Top |
6. Back to top |
Author: ylbtech Source: http://ylbtech.cnblogs.com/ This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities. |