Tutorials Icon

Tutorials > C# Bools

C# Bools

By Stephen Armstrong // November 28th, 2019

Share on FacebookShare on TwitterShare on LinkedInShare on Reddit

Learn about bools and how to use them.

True or False: Bools are very simple. That's not a question, those are the two potential values of a Bool.

Bools are probably the easiest concept in programming to explain, so let's go directly to some examples.

Example code

A bool can be either true or false.

Please note that in C# If statements you do not use (bool==true) or (bool==false).

See below for a quick rundown of how bools can be set, used, and toggled.

  • // Create a bool. Starting value is False.
  • bool exampleBool = false;
  • // If exampleBool is false, then make it True.
  • if (!exampleBool) { exampleBool= true; }
  • System.Console.WriteLine("exampleBool is: {0}", exampleBool);
  • // If exampleBool is True, then toggle its value.
  • if (exampleBool) { exampleBool = !exampleBool; }
  • System.Console.WriteLine("exampleBool is now: {0}", exampleBool);

If you run this code and check the Output, it will show the changed states of the exampleBool.

Ideal usage

Due to their lightweight and simple nature, bools are easy to use in situations where something must be active/inactive.

When making a game (such as in MonoGame or Unity), Bools are bound to be a commonly-used type to determine game states (if the game is paused or not, if enemies have been killed, and much more).

< Go back

Return to top of page

Article Icon

Welcome to Industrian.net!

On this website you'll find more information about our games, and also some tutorials for you to start making games of your own! You can also follow us on various social platforms!