C# bool Keyword with Example

In this tutorial, we will learn about the bool keyword, its usage, memory size with example in C#. By IncludeHelp Last updated : April 04, 2023

C# bool Keyword

The bool keyword used to declare a variable that can store Boolean values true or false. The bool keyword is an alias of System.Boolean.

It occupies 1 byte (8 bits) in the memory.

Syntax

bool variable_name = value;

There are only two possible values that can be assigned to a bool variable 1) true or 2) false

Example of bool keyword in C#

Here, we are declaring a bool variable answer, initializing it with the value true and printing its value, type, and size of a bool type variable.

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //variable declaration
      ulong num = 12345;

      //printing value
      Console.WriteLine("num: " + num);
      //printing type of variable
      Console.WriteLine("Type of num: " + num.GetType());
      //printing size
      Console.WriteLine("Size of a ulong variable: " + sizeof(ulong));
      //printing minimum & maximum value of ulong
      Console.WriteLine("Min value of ulong: " + ulong.MinValue);
      Console.WriteLine("Max value of ulong: " + ulong.MaxValue);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

answer: True
Type of answer: System.Boolean
Size of a bool variable: 1



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.