Home » C#.Net

bool keyword in C#

C# bool keyword: Here, we are going to learn about the bool keyword in C#, what is bool keyword, how to use it in C#?
Submitted by IncludeHelp, on March 06, 2019

C# bool keyword

In C#, bool is a keyword which is used to declare a variable that can store Boolean values true or false. 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

C# code to demonstrate example of bool keyword

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)
        {
            //bool variable declaration
            bool answer = true;

            //printing value
            Console.WriteLine("answer: " + answer);
            //printing type of variable
            Console.WriteLine("Type of answer: " + answer.GetType());
            //printing size of a bool 
            Console.WriteLine("Size of a bool variable: " + sizeof(bool));

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

Output

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


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.