C # is pronounced as C sharp. The .NET framework is employed to execute this object-oriented programming language. Microsoft has created it and this language belongs to the C family. The C# language is used to create a wide range of applications, including desktop apps, web apps, and games. C# has many datatypes and one of them is the C# bool type. The type and size of variable values are specified by the data type. A ‘bool’ keyword can be used to represent a Boolean data type. The keywords in a language define a predefined actions or some built-in process. A ‘bool’ data type stores 1 byte (8 bits) in the memory. In a programming language, we need a data type that stores only two values that can be true or false, yes or no, and on or off. So, the ‘Bool’ data type has this specific property to store two values of true or false.
Parameters that are utilized in the C# bool type are as follows:
- Bool Variable name: The ‘variable name’ represents any variable like x, y, z.
- Value: Value represents the two values either it can be true or false. It is because of the Boolean type that represents truth and is used in the expression.
Usage of C# Bool Type
C# bool type is used in different methods to represent the truth values. Some methodologies return the ‘bool’ values and these are:
- Bool Array: The C# Boolean arrays are simple and allow safe and clean code. They are not memory-efficient but are good enough
- Bool Sort: The C# Boolean can be sorted. We can use this to sort the entries in the collection according to true or false. If the elements are retained in an array or list then they are arranged by using the Sort method.
- Bool Parse: Strings can be converted into a ‘bool’. And this is done by using the bool parse method or bool. Try Parse.
In C# bool type programs, only one value is returned that can be true or false.
Example no. 1:
In this instance, we will discuss how to get the result in the bool type by comparing two integer values.
Let us execute a simple C# code that shows how this program works. We started by installing the software ‘Visual studio’ and started coding by using .NET Framework. Numerous class libraries that belong to this framework are referred to as framework class libraries. We have given the project name as Example1.
internal class Program
static void Main(string args)
int a= 5;
int b= 4;
Console.WriteLine(a < b);
After utilizing the namespace example1, we give the reference by writing the statement ‘internal class Program’. Here, the ‘class’ is a term that is being utilized to define any class in the code. Next, the static void Main() function is employed. The usage of the ‘static’ keyword shows that the accessibility of static members does not require the object. This method's return type is ‘void’. The term Main() indicates the method name. And this function contains the attribute: string args. For command line arguments, the ‘string args’ attribute will be utilized. During the C# program, we can pass numerous values. Here, we have taken an input variable ‘a’ having integer data type and assigned it to value 5. Then, we have taken another input variable ‘b’ and assigned it to value 4. We have declared these variables to compare them and get an output in bool type. In the next statement, the Console.WriteLine() function is invoked which is used to write a text or output on the terminal. In this code, the values of variables ‘a’ and ‘b’ are compared by using a comparison operator to show whether ‘a’ is less than ‘b’ or not. So, in the outcome, we will get the answer in terms of true or false (bool type). We run our program by using debug and have an output.
The outcome of the aforementioned code is seen here. The return value is ‘false’ because the value of ‘a’ is 5 which is greater than the value of ‘b’, hence we are left with false.
Example no. 2:
Here, we will implement another code to know more about the concept of bool data type. ‘Bool’ datatype stores 1 byte of memory and represents the value either true or false. And it is crucial to utilize the appropriate data type for the variable. Here, we have specified the project name as Example2.
internal class Program
static void Main(string args)
bool isCSharpeasy = true;
bool isCSharpedifficult = false;
The ‘System’ and ‘namespace example2’ are utilized in the first statement of the code. Then we provide the reference by inserting the line ‘internal class Program’. Following that, the static void Main() method is used. We pass the ‘string args’ as a parameter to this method. Now, the C# program allows us to pass values. Within the body of the static void Main() function, we have taken the variable ‘isCsharpeasy’ having ‘bool’ type and set it to true. Similarly, another variable ‘isCsharpedifficult’ is defined as false. In the next statement, we invoked the function Console.WriteLine() twice. The first Console.WriteLine() will represent the value of ‘isCsharpeasy’ and the second one will print the value of ‘isCsharpedifficult’. These functions will display the outcome as true or false. When the variable ‘isCsharpeasy’ is called, then ‘true’ will be shown and when the attribute ‘isCsharpedifficult’ is employed, then ‘false’ will be displayed.
So, here we have output in terms of bool data types that are true and false. When the first Console.WriteLine() is called, it has displayed the value as ‘true’ and the second Console.WriteLine() prints the values as ‘false’.
The introduction, syntax, and usage of the C# bool data type were discussed in this tutorial. Further, we explained the execution of Boolean keywords by performing different programs in visual studio. We also talked about the C# methods that are employed during the execution of the program. Additionally, we run the code in which we compare two integer values to produce a bool-type result. To ensure that users’ understanding of the C# bool type is complete, we explored the topic in detail.