86

Why was it decided that when parsing a boolean, 0/1 are not acceptable?

When parsing any integer type value, it accepts numerical strings to be parsed. (And if .NET can parse the string "One hundred million two hundred and sixty five thousand eight hundred and sixty five" I would be surprised).

What makes booleans special? They are essentially 0 as false, and non-zero as true in my experience...

Is there a bcl method to parse a string like this, and if not, why?

Note: I forgot to specify in a string "0" and "1". Curious though that if already an int it works as I anticipated. Maybe this caused the confusion.

0

7 Answers 7

85

0 and (not-zero) are not equal to "false" and "true", they're just the representation chosen by C. Other languages use 0 for true and -1 for false, or other schemes entirely. A boolean is not a 0 or a 1, it's a true or a false.

Should it also handle "yes" and "no", "off" and "on", and all of the myriad other things that are analogous to booleans? Where would you draw the line?

12
  • 3
    Thanks! This is what I was looking for, an explanation of why. If some languages indeed use 0 for true, and -1 for false, I honestly think they are screwed up, but that is a great reason not to automatically assume through a parse that some developers may use blindly. Dec 14, 2009 at 22:04
  • 90
    Why, yes. Yes it should handle yes/no off/on.
    – WEFX
    Dec 20, 2011 at 16:02
  • 3
    To further explain why some languages use 0 for true and -1 for false, consider their binary representations: 00000000 and 11111111.
    – MCattle
    Mar 6, 2013 at 0:18
  • 8
    @MCattle: WTH? Does 11111111 sound falser to you than 00000000, or even -1? personally i think it's rightfully a matter of definition and talking about personal preference is a bit pointless, but looking at the binary representation did certainly not enhance my intuition on that one...
    – Mike Fuchs
    Apr 26, 2013 at 15:54
  • 5
    I thought that there was a convention: 0 = false, and anything else is true (usually 1, but also -1 if you prefer). So I would say 00000 is false, 1 is true and 1111111 is extremely true! ;). @Aric can you tell me in what language is 0=true?. The only example that comes to my mind is the shell, where you usually return(0) when the program finishes, but this is actually an error code (erro=0 -> error=false, so ok). Nov 8, 2013 at 11:59
36

What makes booleans special? They are essentially 0 as false, and non-zero as true in my experience...

That is an implementation detail, and isn't at all relevant.

true is a boolean value. false is a boolean value. Anything else is not.

If you want to parse something such that the string "0" evaluates false while anything else evaluates true, you can use:

!mystr.Equals("0");
2
  • Nice concise solution there, me likey likey!
    – RemarkLima
    Jan 7, 2013 at 20:24
  • 10
    And the string "true" and the string "false" are strings, not booleans. Yet nobody seems to have a problem recognizing their meaning when converting them to booleans. If we're smart enough to recognize that "true" means true then I think we're smart enough to recognize that "1" and 1 mean true, too. And we're smart enough to recognize that "on" and "yes" are affirmatives which can also be safely converted to true if the programmer is specifically requesting a conversion..
    – Rikaelus
    Feb 23, 2015 at 3:35
22

The shared FormatHelperclass shown below provides a simple solution using two variations of an overloaded method called StringToBoolean.

FormatHelper.StringToBoolean(String value)
FormatHelper.StringToBoolean(String value, Boolean NullOrEmptyDefault)

Both variations provide a case-insentive string match

1) The normal convertion from string to boolean defaulting empty or null strings to false

The following examples will result in a boolean value of false:-

 Boolean myBool = FormatHelper.StringToBoolean("");
 Boolean myBool = FormatHelper.StringToBoolean("0");
 Boolean myBool = FormatHelper.StringToBoolean("false");
 Boolean myBool = FormatHelper.StringToBoolean("False");
 Boolean myBool = FormatHelper.StringToBoolean("no");
 Boolean myBool = FormatHelper.StringToBoolean("off");

All other string values will result in a Boolean value of true such as:-

 Boolean myBool = FormatHelper.StringToBoolean("1");
 Boolean myBool = FormatHelper.StringToBoolean("true");
 Boolean myBool = FormatHelper.StringToBoolean("True");
 Boolean myBool = FormatHelper.StringToBoolean("yes");
 Boolean myBool = FormatHelper.StringToBoolean("xyz blah");

Note: Edit the value of BooleanStringOff in the class below to include more (or less) values for false/off

2) Follows the same rules as 1) above but allows a default value of true to be supplied as the 2nd argument to the conversion.

The default value is used when the String value is empty or null. This is useful if a missing string value needs to signify a true state.

The following code example will return true

 Boolean myBool = FormatHelper.StringToBoolean("",true);

The following code example will return false

 Boolean myBool = FormatHelper.StringToBoolean("false",true);

This is the code for the FormatHelper class

public class FormatHelper
{
    public static Boolean StringToBoolean(String str)
    {
        return StringToBoolean(str, false);
    }

    public static Boolean StringToBoolean(String str, Boolean bDefault)
    {
        String[] BooleanStringOff = { "0", "off", "no" };

        if (String.IsNullOrEmpty(str))
            return bDefault;
        else if(BooleanStringOff.Contains(str,StringComparer.InvariantCultureIgnoreCase))
            return false;

        Boolean result;
        if (!Boolean.TryParse(str, out result))
            result = true;

        return result;
    }
}
1
10

Unfortunately, this happens a lot in .NET. For example, I can't remember if it's XML Serializer or XmlConvert but one of them fails if the casing of True/False are not correct.

You can round trip through integer to get what you want.

string s = "2";
int i = Convert.ToInt32(s);
bool b = Convert.ToBoolean(i);

In the above case, anything non-zero will evaluate to true.

For this reason, I created a class I use all over called ConversionStrategy which takes into account the source type and destination type and chooses the most ideal (and flexible) conversion strategy for making the conversion.

2
  • 2
    The accepted answer sounds nice in theory, but when you see stuff like this there's really no rationalization that can make this make sense.
    – Kyle
    Feb 7, 2012 at 21:32
  • faster solution
    – elle0087
    Apr 9, 2021 at 9:39
7

You want Convert.ToBoolean(int value) not sure what's up with the Parse methods :-)

Code for no useful purpose:

        int falseInt = 0;
        int trueInt = 1;

        bool falseBool;
        bool trueBool;

        if (bool.TryParse(falseInt.ToString(), out falseBool))
        {
            if (!falseBool)
            {
                MessageBox.Show("TryParse: False");
            }
        }

        if (bool.TryParse(trueInt.ToString(), out trueBool))
        {
            if (!trueBool)
            {
                MessageBox.Show("TryParse: True");
            }
        }

        falseBool = Convert.ToBoolean(falseInt);
        trueBool = Convert.ToBoolean(trueInt);

        if (!falseBool)
        {
            MessageBox.Show("Convert: False");
        }

        if (trueBool)
        {
            MessageBox.Show("Convert: True");
        }
5

How about this?

  byte i = 1; //or 0
  bool myBool = BitConverter.ToBoolean(new byte[] { i }, 0)
1

To answer the question would be hard. Maybe because the narrow minded developers at Microsoft had their own reasons? No disrespect intended to them. Just saying they did not think about what it would need to be used for or how it would be used. I can't think of a reason why my extension below would not work for anyone. I mean a Boolean value is either on or off, true or false. In my mind it's basically binary. Parse methods for Int, Double, Char, Long, Byte, etc are more forgiving with their Parse methods.

However, consider this; You are looking to see if a value exists in an object. The same might be said for the following...

string myvar = "empty"; //Or maybe = "NULL"
if (String.IsNullOrEmpty(myvar))
{
    //Should this be true?
}

Anyway, let's just make this simple. Here is my solution using an extension method to create a ToBoolean() method for a string.

using System.Linq;
public static bool ToBoolean(this string input)
{
    //Define the false keywords
    String[] bFalse = { "false", "0", "off", "no" };

    //Return false for any of the false keywords or an empty/null value
    if (String.IsNullOrEmpty(input) || bFalse.Contains(input.ToLower()))
        return false;

    //Return true for anything not false
    return true;
}
1
  • To add to my post, I just happen to run across something on this subject today. Even SQL handles Boolean with 1's and 0's. BOOL: Zero is considered as false, nonzero values are considered as true.
    – Arvo Bowen
    Aug 19, 2019 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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