C# True

True keyword

True indicates yes or positive. What are some ways you can use the true boolean constant in the C# programming language? This constant provides a way to indicate truth values in variables. We specify true using lowercase keywords.

Keywords

This C# article describes the true constant. It presents an example program.

Examples

Note

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. In the C# language, the if-statement requires that its expression be evaluated in a bool context, which means you may sometimes need to explicitly specify the comparison.

Program that uses true [C#]

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
Bool keyword

Description. 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. This site has more information on the bool type.

Summary

The true keyword in the C# programming language 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—in some cases.

False Value Bool Type
.NET