20

I've been reading about overloading true and false in C#, and I think I understand the basic difference between this and defining a bool operator. The example I see around is something like:

public static bool operator true(Foo foo) {
  return (foo.PropA > 0);
}
public static bool operator false(Foo foo) {
  return (foo.PropA <= 0);
}

To me, this is the same as saying:

public static implicit operator bool(Foo foo) {
  return (foo.PropA > 0);
}

The difference, as far as I can tell, is that by defining true and false separately, you can have an object that is both true and false, or neither true nor false:

public static bool operator true(Foo foo) { return true; }
public static bool operator false(Foo foo) { return true; }
//or
public static bool operator true(Foo foo) { return false; }
public static bool operator false(Foo foo) { return false; }

I'm sure there's a reason this is allowed, but I just can't think of what it is. To me, if you want an object to be able to be converted to true or false, a single bool operator makes the most sense.

Can anyone give me a scenario where it makes sense to do it the other way?

Thanks

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • I've just found these operators as well and did some tests. It seems like `if` only uses the `true` operator, and `if (!myObj)` is not possible at all, so there's no point (yet) in implementing a contradictory `false` operator. Did I miss anything? How can I use the `false` operator at all? – ygoe Sep 14 '13 at 12:31

4 Answers4

13

As the docs say, overloading true and false is intended to support (nullable) database-types (Yes/No, Y/N, 0/1, etc).

And of course you can define them inconsistently, as with any operator. It is your responsibility to return something sensible. The compiler goes no further than requiring neither or both.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Thanks - I didn't get that far in MSDN - I started looking elsewhere after their [operator overloading](http://msdn.microsoft.com/en-us/library/aa288467(VS.71%29.aspx "operator overloading") page, which wasn't much help. I suppose I didn't think about the fact that you can overload ==/!= the same way, where they conflict with each other, I just could never imagine actually doing it. Thanks for the tip. – Joe Enos Apr 19 '10 at 22:04
8

I had no idea these operators existed. That means you can implement the self-negation paradox:

public class ThisClassIsFalse
{
    public static bool operator true(ThisClassIsFalse statement)
    {
        return statement ? false : true;
    }

    public static bool operator false(ThisClassIsFalse statement)
    {
        return statement ? true : false;
    }
}

So now we know the true solution to this classic paradox... StackOverflowException.

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
2

I've seen people overload the true and false overloads in order to do clever things like building expressions in .NET 2.0, before Linq existed.

Ayende worked out a syntax like this to build NHibernate criteria queries, using his NHQG project:

return Repository.FindAll(
    (Where.Publisher.Name == name) &&
    (Where.Publisher.City == city));
Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
  • 5
    Ahh, the days before LINQ...Those were innocent times when people did atrocious things that were clever at the time... – Joe Enos Apr 19 '10 at 22:08
1

Depending on the system, true can be any non-zero value. In others, it can be any positive value.

Other systems aren't truly boolean, and allow a third state null or nill for the boolean values, which is why you might overload true and false, versus overloading a single bool operator.

Benjamin Anderson
  • 1,376
  • 10
  • 15
  • This is specifically for .NET, so are you saying that it's for .NET applications talking to non-.NET code? – Joe Enos Apr 19 '10 at 21:35
  • Joe, this is a feature in the specification for C#, not .NET. C# could be used in any system, and true and false might need their definition changed. – Benjamin Anderson Apr 20 '10 at 14:18
  • Benjamin: Thanks for the response - a few comments: First, I believe this is the case for other .NET languages, not just C# - VB.NET has special operators IsTrue and IsFalse which seem to be the same thing, unless I'm mistaken. Also, you mentioned that C# could be used in any system - I've heard a few people say things like this in the past, but I've never heard explanations. Other than Mono, is there any other place outside of .NET where C# is actually used? Thanks – Joe Enos Apr 20 '10 at 15:45
  • Mono is the primary alternative implementation of C#, but C# the language is an open ISO standard, and can be used and implemented by anyone that wants to do the work to implement it. – Benjamin Anderson Apr 20 '10 at 20:25