bool
In this chapter you will learn:
- Get to know bool type
- String representing the true and false value
- Output a boolean variable with Console.WriteLine
bool type
The bool type represents true/false values. C# defines the values true and false using the reserved words 'true' and 'false'.
A variable or expression of type bool will be one of these two values. There is no conversion defined between bool and integer values.
C# bool type (System.Boolean) can only take two values: true or false.
The result of comparison operators: ==, >, <, >=, <= is bool type value.The following code defines a bool variable and assign the result of a comparison to it.
using System;/*from j a v a 2s . co m*/
class Operators {
static void Main() {
bool a = 4 > 5;
Console.WriteLine("{0}", a);
}
}
The following code compares two double values.
using System;/*from ja v a 2s .c om*/
class Program
{
static void Main(string[] args)
{
double d = 1D;
double d2 = 0.1D;
bool result = d == d2;
Console.WriteLine("result is "+ result);
}
}
The result:
String representing the true and false value
The following code outputs the bool
FalseString
, TrueString
.
using System;//from j a v a 2 s.c o m
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
bool b3 = true; // No problem.
bool b4 = false; // No problem.
Console.WriteLine("-> bool.FalseString: {0}", bool.FalseString);
Console.WriteLine("-> bool.TrueString: {0}", bool.TrueString);
}
}
Output a boolean variable with Console.WriteLine
using System; //from j a v a 2 s . co m
class Example {
public static void Main() {
Console.WriteLine("10 > 9 is " + (10 > 9));
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: