C Sharp (C#) Bool Data Type


The bool datatype represents true/false values. The variable can hold only a true or a false (these two are C# keywords). The bool value is not automatically converted to either 1 or 0. When used bool in an If statement, there is no necessary to check if its true or false using assignment operator ( = ). Have a look at the examples and will be easier to understand.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            bool a = true;
            bool b = false;

            Console.WriteLine("The value of a is : {0}", a);
            Console.WriteLine("The value of b is : {0}", b);

            Console.ReadLine();
           
            //using it in an if condition
            if (a)
            {
                Console.WriteLine("If a is true , I will be displayed.");
            }
            else
            {
                Console.WriteLine("If a is false, I will be displayed.");
            }

            if (b)
            {
                Console.WriteLine("If b is true , I will be displayed.");
            }
            else
            {
                Console.WriteLine("If b is false, I will be displayed.");
            }

            Console.ReadLine();
           
        }
    }
}

Output :

No comments: