Lesson 2.16

bool data type C# - programming example


Objective

What is bool data type in c#?
How to declare and initialize
Programming examples

What is Bool Data Types in C#?

"The bool data type stores true or false only. You can only assign true or false to its variable. The bool keyword is an alias of System.Boolean."

TypeRangeSize.Net Framework type
boolTrue or False--System.Boolean

Declaration and Initialization

You can declare bool data type as follow:

		  
  bool check = true;

		  

Programming Example

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

namespace bool_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            bool check = false;
            string passcode = "pass123";

            Console.WriteLine("Enter Password:\t");
            check = (passcode == Console.ReadLine());

            if (check)
            {
                Console.WriteLine("\n\nSuccessfully Login");
            }
            else
            {
                Console.WriteLine("\n\nInvalid Password");
            }
            Console.ReadKey(); 
        }
    }
}

     	  

Output

Enter Password:
pass123

Successfully Login
_

Summary

The bool data type stores only two values true or false. It is used to evaluate value based on true or false literals. It is an alias of System.Boolean. In the next chapter we will discuss on object in c#.

BackNext