C# Booleans

C# booleans are a a simple way to hold a value of true or false. These are rather quite simple.

we can instantiate a boolean by doing: bool myVar;. We do not need to define a default value if we do not want. However to use a default value we can do:

bool isFinished = false;
Console.WriteLine(isFinished); // will show false.

Now, we can use the boolean to determine if we should move forward with a if statement with the following.

bool isFinished = false;

if(isFinished == false){
CheckForResult(); // this is just a example
}

If you noticed we did a == false. The nice thing about C# is we do not need to define this. We can either do an if(variable) or if(!variable). Using the “!” will mean if false.

Example:

bool isFinished = false;

if(!isFinished){
    Console.WriteLine("Were not finished");
}
else if(isFinished){
    Console.WriteLine("Were Finished!");
}

Leave a Reply

Your email address will not be published. Required fields are marked *