TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# True and False

This C# article describes true and false. It presents example programs.

True, false. True and false are boolean literals.

They are values that mean yes and no. They can be stored in variables of type bool. In the C# language, true and false are lowercase reserved keywords.

Bool

True indicates yes or positive. To start, we look at a program that uses true in different program contexts. Most importantly, we see that you can use true in if-statements, while-statements, expressions, assignments and negations.

Tip: In the C# language, the if-statement requires that its expression be evaluated in a bool context.

And: This means you may sometimes need to explicitly specify the comparison—you cannot directly test integers.

C# program that uses true

using System;

class Program
{
    static void Main()
    {
	// Reachable.
	if (true)
	{
	    Console.WriteLine(1);
	}
	// Expressions can evaluate to true or false.
	if ((1 == 1) == true)
	{
	    Console.WriteLine(2);
	}
	// While true loop.
	while (true)
	{
	    Console.WriteLine(3);
	    break;
	}
	// Use boolean to store true.
	bool value = true;
	// You can compare bool variables to true.
	if (value == true)
	{
	    Console.WriteLine(4);
	}
	if (value)
	{
	    Console.WriteLine(5);
	}
    }
}

Output

1
2
3
4
5

Program review. Most parts of this program are clear to understand. You can change the value of a bool that is set from assignment to an expression by applying a == true or != true at the end.

The true keyword is very frequently used. There are some subtleties to its use, particularly in assignments to expressions. If-statements and while-statements require a bool processing context, which mandates the usage of true—or false.

IfWhile

False is not true. The false keyword in the C# language is a constant boolean literal, meaning it is a value that can be stored in a variable. It can also be evaluated in an if-expression.

In the first if-statement, we see that when an expression evaluates to false, the code inside the if-block is not reached. You can set a bool variable to false. You can invert the value of a bool variable with the exclamation operator.

Finally: The expression != false is equal to == true. This can be entirely omitted.

C# program that uses false literal

using System;

class Program
{
    static void Main()
    {
	// Unreachable.
	if (false)
	{
	    Console.WriteLine(); // Not executed.
	}
	// Use boolean to store true and false.
	bool value = true;
	Console.WriteLine(value); // True

	value = false;
	Console.WriteLine(value); // False

	value = !value;
	Console.WriteLine(value); // True

	// Expressions can be stored in variables.
	bool result = (1 == int.Parse("1")) != false;
	Console.WriteLine(result); // True
    }
}

Output

True
False
True
True

Tip: It is usually clearer to express conditions in terms of truth rather than testing against false, but sometimes false may be clearer.

So: If false is the expected result, using == false may be better. False can be stored in the bool variable type.

Bool. True and false are stored in bool variables. Bools are not directly convertible to value types such as int. Instead, a special conversion method must be used. More information is available about how to convert bools.

Convert Bool, Intbool.Parse

Summary. True and false are commonly used values. They can be stored in a variable of type bool. They can be used anywhere a boolean expression is used—this includes an if-statement or a while-loop.

Also: True and false cannot be directly converted to other values such as 1 and 0.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf