Illustration of Operator Keywords (true, false) with Examples in C# (C Sharp)

In this article you will review the purpose and usage of two operator keywords namely true and false. Most of you might have used true and false literals for assigning Boolean values and for performing conditional checks. In addition to that, you will learn about overloading true and false operators in this article. In general, both true and false can be classified as:

• literals

• operators

You will analyze about each of these in detail.

Literals - true, false:

These literals refer to the true and false keywords that you commonly use to represent the corresponding Boolean values. Here is a simple example:

class sampleClass {
int data;
sampleClass(int data){
this.data = data;
}
public bool checkEven( ){
bool retValue;
if(data % 2 == 0) {
retVal = true;
}
else {
retVal = false;
}
return retVal;
}
public static void Main() {
sampleClass obj = new sampleClass(10);
bool result = obj.checkEven();
Console.WriteLine(result);
}
}

Output of this code will be:
true

In this example, you return true if the number is even. If it is odd, you display false. To achieve this functionality, in checkEven( ) method you assign the retval to either true or false based on the conditional check. These true and false values that you directly represent in the code are the literals with which you represent Boolean values.

Operators – true, false:

Apart from assigning the values true and false, you might also be performing conditional checks wherein the conditions will be evaluated to either one of these values. Here is a very simple example for such conditional checks:
class sampleClass {
public static void Main(){
bool condition1 = true;
if(condition1) {
Console.WriteLine(“condition1 is true”);
}
else {
Console.WriteLine(“condition1 is false”);
}
}
}
Output of this code is straight forward. It will be:

condition1 is true

An if-condition will generally include expressions which evaluate to a Boolean value. In this example, you don’t have any expression, but you just mention condition1 inside if-condition instead of an expression. But this is legal because the statement:

if(condition1)

will be interpreted as:

if(condition1 == true)

However this flexibility is available only for Boolean types. Check the following example:

class sampleClass {
public static void Main() {
sampleClass obj = new sampleClass();
if(obj) {
Console.WriteLine(“obj evaluates to true”);
}
else {
Console.WriteLine(“obj evaluates to false”);
}
}
}

This is invalid and it will end up in error. An if-condition can include a single variable name or a condition if and only if the variable or condition evaluates to a Boolean value. Mostly all of you know that the above example is incorrect. But to your surprise, you can make the above example legally correct and working. To do that, you have to overload the true and false operators inside your class. Here is a modified example:

class sampleClass {
int data;
public sampleClass(int data) {
this.data = data;
}
public static bool operator true(sampleClass obj) {
return (obj.data % 2 == 0);
}
public static bool operator false(sampleClass obj) {
return (obj.data % 2 != 0);
}
public static void Main() {
sampleClass obj = new sampleClass(100);
if(obj) {
Console.WriteLine(“obj evaluates to true. obj’s data is even.”);
}
else {
Console.WriteLine(“obj evaluates to false. obj’s data is odd.”);
}
}
}

Now the code will work successfully and its output will be:

obj evaluates to true. obj’s data is even.

This is because, when you specify the object obj in the if-condition, the object’s overloaded true operator will be triggered.

When you are overloading true and false operators, following guidelines have to be followed:

• You should overload both true and false operators and not just one of them.

• The return value of the overloaded true and false operator should always be a Boolean.

Also note that, you can code similar logic in do, while, for statements as well.

| How does Yield Keyword of C# Use Lazy Evaluationm: Reference Types |How to Create Iterator to Process Generic List in C# | How to Use “add” and “remove” Contextual Keywords of C# | How to Use ForEach with Arrays in C# |Illustration of Operator Keywords (as, is) with Examples in C# | Illustration of Operator Keywords (new, sizeof) with Examples in C# |Illustration of Operator Keyword stackalloc with Examples in C# |Illustration of Operator Keywords (true, false) with Examples in C#|Illustration of Operator Keyword typeof with Examples in C# |List of Contextual Keywords in C#|


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.