[ACCEPTED]-What are true and false operators in C#?-operators

Accepted answer
Score: 13

You would overload the true or false operators if you were defining a specialized boolean value. This is not typically needed, however, which 11 is why they don't seem useful. For example, in 10 a fuzzy-logic boolean class, you might have 9 something like this:

// Represents a boolean that can store truth values in a range from [0, 1], with
//   a value of one indicating complete metaphysical certitude and a value of
//   zero indicating complete impossibility.
public class FuzzyBoolean {
// ...
   public static bool operator true(FuzzyBoolean fb) {
      return fb.TruthValue > 0;
   }

   public static bool operator false(FuzzyBoolean fb) {
      return fb.TruthValue == 0;
   }
// ...
}

Note that if you overload 8 true, you must also overload false (and vice versa).

Of 7 course, there are also the true and false literals, the two 6 literal values you can assign to a boolean 5 instance. Don't confuse these with the operators 4 mentioned above. A more substantial example 3 of how you'd use this, involving booleans 2 in a database, is given in the MSDN docs 1 here.

Score: 13

The true and false operators can be overloaded, to 3 allow a class to represent its own state 2 as true or false, for example:

public class MyClass
{
    //...
    public static bool operator true(MyClass op)
    {
        // Evaluation code...
    }

    public static bool operator false(MyClass op)
    {
        // Evaluation code...
    }
}

And you will 1 be able to use the operator in boolean expressions:

MyClass test = new MyClass(4, 3);
if (test)
    Console.WriteLine("Something true");
else
    Console.WriteLine("Something false");

string text = test ? "Returned true" : "Returned false";
Score: 4

See the referenced example in the article

C# Language Specification -- Database boolean type

Essentially 3 these operators allow an instance of a type 2 to be used in boolean conditional logic 1 such as && and ||.

Score: 2

They allow you to overload them using the 2 operator overloading syntax so that a type 1 you define can be interpreted as a boolean.

Score: 2

Prior to C# 2.0, the true and false operators 15 were used to create user-defined nullable 14 value types that were compatible with types 13 such as SqlBool. However, the language now 12 provides built-in support for nullable value 11 types, and whenever possible you should 10 use those instead of overloading the true 9 and false operators. For more information.With 8 nullable Booleans, the expression a != b 7 is not necessarily equal to !(a == b) because 6 one or both of the values might be null. You 5 need to overload both the true and false 4 operators separately to correctly identify 3 the null values in the expression. The following 2 example shows how to overload and use the 1 true and false operators.

Score: 0

The allow you to use a custom type as a 2 part of logic operations; For example, as 1 part of an if or while statement.

More Related questions