tags:

views:

2276

answers:

6

There are two weird operators in C#:

If I understand this right these operators can be used in types which I want to use instead of a boolean expression and where I don't want to provide an implicit conversion to bool.

Let's say I have a following class:

    public class MyType
    {
        public readonly int Value;

        public MyType(int value)
        {
            Value = value;
        }

        public static bool operator true (MyType mt)
        {
            return  mt.Value > 0;
        }

        public static bool operator false (MyType mt)
        {
            return  mt.Value < 0;
        }

    }

So I can write the following code:

    MyType mTrue = new MyType(100);
    MyType mFalse = new MyType(-100);
    MyType mDontKnow = new MyType(0);

    if (mTrue)
    {
         // Do something.
    }

    while (mFalse)
    {
        // Do something else.
    }

    do
    {
        // Another code comes here.
    } while (mDontKnow)

However for all the examples above only the true operator is executed. So what's the false operator in C# good for?

Note: More examples can be found here, here and here.

A: 

I have no idea, but I just wanted to back my upvote up with a "Great question!" I'll be watching this.

Have you tried:

if(!mTrue) {
  //whatever
}
Chris Marasti-Georg
The code above will not compile because MyType cannot be implicitly casted to bool.
Jakub Šturc
+12  A: 

The page you link to http://msdn.microsoft.com/en-us/library/6x6y6z4d.aspx says what they were for, which was a way of handling nullable bools before nullable value types were introduced.

I'd guess nowadays they're good for the same sort of stuff as ArrayList - i.e. absolutely nothing.

Will Dean
+3  A: 

It appears from the MSDN article you linked to it was provided to allow for nullable boolean types prior to the Nullable (i.e. int?, bool?, etc.) type being introducted into the language in C#2. Thus you would store an internal value indicating whether the value is true or false or null, i.e. in your example >0 for true, <0 for false and ==0 for null, and then you'd get SQL-style null semantics. You would also have to implement a .IsNull method or property in order that nullity could be checked explicitly.

Comparing to SQL, imagine a table Table with 3 rows with value Foo set to true, 3 rows with value Foo set to false and 3 rows with value Foo set to null.

SELECT COUNT(*) FROM Table WHERE Foo = TRUE OR Foo = FALSE
6

In order to count all rows you'd have to do the following:-

SELECT COUNT(*) FROM Table WHERE Foo = TRUE OR Foo = FALSE OR Foo IS NULL
9

This 'IS NULL' syntax would have equivilent code in your class as .IsNull().

LINQ makes the comparison to C# even clearer:-

int totalCount = (from s in MyTypeEnumerable
                 where s || !s
                 select s).Count();

Imagining that MyTypeEnumberable has exactly the same contents of the database, i.e. 3 values equal to true, 3 values equal to false and 3 values equal to null. In this case totalCount would evaluate to 6 in this case. However, if we re-wrote the code as:-

int totalCount = (from s in MyTypeEnumerable
                 where s || !s || s.IsNull()
                 select s).Count();

Then totalCount would evaluate to 9.

The DBNull example given in the linked MSDN article on the false operator demonstrates a class in the BCL which has this exact behaviour.

In effect the conclusion is you shouldn't use this unless you're completely sure you want this type of behaviour, it's better to just use the far simpler nullable syntax!!

Update: I just noticed you need to manually override the logic operators !, || and && to make this work properly. I believe the false operator feeds into these logical operators, i.e. indicating truth, falsity or 'otherwise'. As noted in another comment !x won't work off the bat; you have to overload !. Weirdness!

kronoz
+3  A: 

AFAIK, it would be used in a test for false, such as when the && operator comes into play. Remember, && short-circuits, so in the expression

if ( mFalse && mTrue) 
{
   // ... something
}

mFalse.false() is called, and upon returning true the expression is reduced to a call to 'mFalse.true()' (which should then return false, or things will get weird).

Note that you must implement the & operator in order for that expression to compile, since it's used if mFalse.false() returns false.

@nird.myopenid.com - wow, that's cool! And horrible! But cool...

Shog9
+25  A: 

You can use it to override the && and || operators.

The && and || operators can't be overridden, but if you override |, &, true and false in exactly the right way the compiler will call | and & when you write || and &&

for example, look at this code (from http://www.ayende.com/Blog/archive/2006/08/04/7381.aspx - where I found out about this trick):

public static AbstractCriterion operator &(AbstractCriterion lhs, AbstractCriterion rhs)
{
       return new AndExpression(lhs, rhs);
}

public static AbstractCriterion operator |(AbstractCriterion lhs, AbstractCriterion rhs)
{
       return new OrExpression(lhs, rhs);
}

public static bool operator false(AbstractCriterion criteria)
{
       return false;
}
public static bool operator true(AbstractCriterion criteria)
{
       return false;
}

This is obviously a side effect and not the way it's intended to be used, but it is useful.

Nir
+1 That's a cool trick. Nice find!
Tormod Fjeldskår
+7  A: 

Shog9 and Nir: thanks for your answers. Those answers pointed me to Steve Eichert article and it pointed me to msdn:

The operation x && y is evaluated as T.false(x) ? x : T.&(x, y), where T.false(x) is an invocation of the operator false declared in T, and T.&(x, y) is an invocation of the selected operator &. In other words, x is first evaluated and operator false is invoked on the result to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value previously computed for x. Otherwise, y is evaluated, and the selected operator & is invoked on the value previously computed for x and the value computed for y to produce the result of the operation.

Jakub Šturc