3

I asked this question on Stack Overflow and it was met with negative reception. I figured that Stack Overflow wasn't the right place to ask this question, so I decided to post it here.

Beside making types like SqlBoolean act more naturally, is there any other reason? If making three state boolean types is the only purpose of this decision, is it a good enough reason to add a feature that is so rarely used to a language?

Here's an example:

public class ThreeStateBool
{
    private bool? value;

    public ThreeStateBool(bool? value)
    {
        this.value = value;
    }

    public static bool operator false(ThreeStateBool tsb)
    {
        return tsb.value == false;
    }

    public static bool operator true(ThreeStateBool tsb)
    {
        return tsb.value == true;
    }
}

Now that I've overloaded true and false, I can use this object in a conditional statement like:

if (new ThreeStateBool(false))
    Console.WriteLine("false");
else if (new ThreeStateBool(true))
    Console.WriteLine("true");
else if(new ThreeStateBool(null))
    Console.WriteLine("null");
2
  • 2
    The operators make three-state logic work more naturally. As to whether the value of that was worth the effort, I suspect a lot of people would view a lot of language features as being silly relative to the features they feel the language should have but which are still missing.
    – supercat
    Sep 16 '14 at 21:53
  • Considering all the syntax hacks of LINQ to SQL, I wouldn't be surprised if Microsoft added this feature just to get better SQL support.
    – Idan Arye
    Sep 17 '14 at 0:21
2

You have to have a bit of context here: I think the main point missing is that this was a pre C#2.0 feature, which means it's a feature that came before nullable types. The page that describes the overloading of the 'true' operator is clearly geared towards the direction of having a nullable boolean without using a nullable type as support.

But there are other cases where you might want to treat an object instance as a boolean, for example where you want to check if the internal state of an object is ok, and you would want to write something C-style:

if (obj) {
    obj.DoSomething();
}

It's a possibility, it's not a mandatory thing that you'd want to do. It's a tool to be used if needed, and, as you well noted, it is somehow superseded by the use of nullable types; however, you might want to write something similar to a ThreeStateBoolean when you know, for example, that 'false' means 'null or false'. Writing those checks inline every time is a boring affair.

5
  • But that seems like a LOT of work for adding a feature that's not very useful. Is three state booleans the only reason ? I just can't get my head around it I guess.
    – brz
    Sep 17 '14 at 6:27
  • I can't find any other reason; on the other hand, this feature was designed before nullable types. Sep 28 '14 at 17:06
  • Seems more like a hack than feature imo.
    – brz
    Sep 28 '14 at 17:18
  • In the context of nullable types not being present, it's a pretty neat feature. Sep 29 '14 at 17:28
  • It's important to note that Unity does precisely this: docs.unity3d.com/ScriptReference/Object-operator_Object.html Jan 3 '15 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.