Close

c# – Booleans

booleans are variables that hold true or false.

 

[csharp]

using System;

class BooleanDemo
{
static void Main()
{
bool myFirstBoolean = 3 + 2 == 5;
Console.WriteLine(myFirstBoolean.ToString());

bool mySecondBoolean = 3 + 2 => 5;
Console.WriteLine(mySecondBoolean.ToString());

// Here we use the 'and', "&&" operator
bool BothAreTrue = myFirstBoolean && mySecondBoolean ;
Console.WriteLine("Are both true: " + BothAreTrue.ToString());

// Here we use the 'or', "||" operator
bool IsOneTrue = myFirstBoolean || mySecondBoolean ;
Console.WriteLine("Are both true: " + IsOneTrue.ToString());

// here we create a boolean that stores the opposite result
// of another boolean using the "not" (!) operator:
bool GetReverseInfo = !myFirstBoolean;
Console.WriteLine("The opposite result of myFirstBoolean, is: " + GetReverseInfo.ToString());

}
}
[/csharp]