tags:

views:

966

answers:

5

What is the purpose and effect of the true and false operators in C#? The official documentation on these is hopelessly non-explanatory.

+4  A: 

See the referenced example in the article

C# Language Specification -- Database boolean type

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

JaredPar
+2  A: 

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

brianary
A: 

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

Timothy Carter
+8  A: 

You would overload the true or false operators if you were defining a specialized boolean value. This is not typically needed, however, which is why they don't seem useful. For example, in a fuzzy-logic boolean class, you might have 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 true, you must also overload false (and vice versa).

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

John Feminella
This seems like a very contrived example. I would see the use of something like this for objects having an OK and a bad state - say a file object could return true if it was in a readable state, allowing one to write a C like 'if (file) { /* do something */ }` construct.
Ori Pessach
@Ori: IMO, that's a terrible idea. A file is not a boolean and should not be used as such. If you want to check if it's in a good state, that should be a boolean method: "file.IsValid()" or something similar.
John Feminella
Neither are FuzzyBooleans, whatever you name them. Using various things as booleans is idiomatic in certain languages. It might not be in C#, but given the extensive facilities to support casting to boolean I suspect that something like that was intended, rather than the ability to define
Ori Pessach
a new boolean type, which I suspect would be pretty rare.
Ori Pessach
My point was that the distance between a FuzzyBoolean and a boolean is much tinier than the distance between the distance between a file and a boolean. C# doesn't prevent you from making implicit conversions to your heart's content; I'm just saying I don't think it's necessarily a great idea.
John Feminella
And I don't necessarily disagree. This is a perfectly acceptable idiom in C, C++ and Python. I don't know enough about C# to tell you whether it would be a good idea or not to do this in C#. I just don't think that the argument "a file is not a boolean" has any merit on its own.
Ori Pessach
To really be argumentative, I could say that an int is not a boolean, and neither is a bit or a voltage level in a circuit. A boolean is a narrowly defined mathematical construct, and its use in a computer language is subject to the whim of the language's designer. As such, what's considered
Ori Pessach
acceptable or not is driven by practical considerations, one of which being readability. Easily casting arbitrary classes to boolean can enhance or hinder readability. We must use good judgment. Think of something like a reference class, which lets you delegate operations to an instance running on
Ori Pessach
another machine - it's rather like a pointer. It might be reasonable to expect people to know what this means: "if (myRef) { myRef.DoSomething(); }". OK, this is getting silly.
Ori Pessach
@Ori: In C# I would say the general consensus is that types stand alone. Unless you have a pretty good reason for an implicit conversion, it's usually considered bad form to supply one. This would make a good SO question, too: "When is it okay to have an implicit conversion?"
John Feminella
Ori Pessach
@Ori: If it weren't lossy, then you could have just used a boolean method anyway. In this case it's lossy because, well, that's the point, to reduce the truth value to a binary state. That's like saying "% is lossy because it only gives you the remainder". But that's the part you care about. :)
John Feminella
We really should make a blog post out of this or something. It's a good discussion. And I wish SO had private messaging!
John Feminella
The problem with a lossy transformation in this case is that a fuzzy truth value of, say, 0.5, has a well defined meaning withing the axioms of fuzzy logic (however you define that.) Squashing that to true or false isn't very useful, while overloading the logic operators for fuzzy values would be.
Ori Pessach
As for private messages, I'm sure you could figure out my email address if you worked at it a bit.
Ori Pessach
+4  A: 

The true and false operators can be overloaded, to allow a class to represent its own state 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 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";
CMS
What if both 'true' and 'false' operators return true?
alex
@alex, in the absence of checks for false in the above example from CMS, I would expect nothing unseemly.
ProfK