NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

bool

The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true and false.

You can assign a Boolean value to a bool variable, for example:

bool MyVar = true;

You can also assign an expression that evaluates to bool to a bool variable, for example:

bool Alphabetic = (c > 64 && c < 123);

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. In C#, there is no conversion between the bool type and other types. For example, the following if statement is illegal in C#, while it is legal in C++:

int x = 123;
if (x) {      // Illegal in C#
   printf("The value of x is nonzero.");
}

To test a variable of the type int, you have to explicitly compare it to a value (for example, zero), that is:

int x = 123;
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 letter. If so, it checks if it is lowercase or uppercase. In each case, the proper message is displayed.

// Character Tester
using System;
public class BoolTest {
   public 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("The character is not an alphabetic character.");
   }
}

Sample Runs

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.

See Also

C# Keywords | Default Values Table | Built-in Types Table