C# Bool Type

In this chapter you will learn:

  1. What is C# Bool Type
  2. How to create bool type variable
  3. if statement with bool variable

Description

The C# bool type is used to contain Boolean values of either true or false. Aliasing System.Boolean type.

You cannot implicitly convert bool values to and from integer values.

Syntax

bool variableName;

Example

if statement with bool variable


using System;/*from w ww.j  a  v  a  2s  .  c om*/

public class MainClass {
  public static void Main( ) {
    bool boolValue1 = true;
    bool boolValue2 = false;


    if( boolValue1 ) 
      Console.WriteLine("You should see this");

    if( boolValue2 )
      Console.WriteLine("You should not see this");

    if( boolValue1 && !boolValue2 )
      Console.WriteLine("Will you see this?");

    if( boolValue1 ) {
      Console.WriteLine("Statement 1");
      Console.WriteLine("Statement 2");  
    }
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is the char type in C#
  2. How to create literal for C# char type
  3. What are the escape sequences
  4. Char Conversions
  5. Example: uses methods from Char type
Home »
  C# Tutorial »
    C# Language »
      C# Data Types
C# Predefined Type Taxonomy
C# Numeric Types
C# Numeric Literals
C# Numeric suffixes
C# float and double
C# Float and Double Special Values
C# decimal
C# Numeric Conversions
C# Bool Type
C# char type
C# String Type