为什么决定在解析布尔值时不接受0/1?
解析任何整数类型值时,它接受要解析的数字字符串。(如果.NET可以解析字符串“ 112.65.865万”,我会感到惊讶)。
是什么让布尔值与众不同?根据我的经验,它们本质上是0,为假,非零为真。
有没有bcl方法可以解析这样的字符串,如果没有,为什么?
注意:我忘了在字符串“ 0”和“ 1”中指定。奇怪的是,如果它已经是一个int,它将按我预期的那样工作。也许这引起了混乱。
Answers:
0和(非零)不等于“ false”和“ true”,它们只是C选择的表示形式。其他语言使用0表示true,使用-1表示false,或者完全使用其他方案。布尔值不是0或1,是对还是错。
它是否还应该处理“是”和“否”,“关闭”和“开启”,以及所有其他类似于布尔值的东西?你会在哪里划界线?
00000000和11111111。
                    是什么让布尔值与众不同?根据我的经验,它们本质上是0,为假,非零为真。
那是一个实现细节,根本不相关。
true是一个布尔值。false是一个布尔值。其他没有。
如果您想解析某些东西,使得字符串“ 0”的计算结果为false其他值true,则可以使用:
!mystr.Equals("0");
FormatHelper下面显示的共享类使用称为的重载方法的两种变体提供了一种简单的解决方案StringToBoolean。
FormatHelper.StringToBoolean(String value)
FormatHelper.StringToBoolean(String value, Boolean NullOrEmptyDefault)
两种变体都提供了区分大小写的字符串匹配
1)从字符串到布尔值的正常转换,默认为空或空字符串 false 
以下示例将导致boolean值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");
所有其他的字符串值将导致Boolean的值true,例如: -
 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");
注意:BooleanStringOff在以下类别中编辑的值,以包含更多(或更少)的false / off值
2)遵循与上述1)相同的规则,但允许将默认值true提供为转换的第二个参数。 
String值为空或时使用默认值null。如果缺少的字符串值需要表示true状态,这将很有用。
以下代码示例将返回 true
 Boolean myBool = FormatHelper.StringToBoolean("",true);
以下代码示例将返回 false
 Boolean myBool = FormatHelper.StringToBoolean("false",true);
这是FormatHelper该类的代码
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;
    }
}
不幸的是,.NET中经常发生这种情况。例如,我不记得它是XML Serializer还是XmlConvert,但是如果True / False的大小写不正确,则其中之一会失败。
您可以遍历整数以获得所需的内容。
string s = "2";
int i = Convert.ToInt32(s);
bool b = Convert.ToBoolean(i);
在上述情况下,任何非零值都将评估为true。
因此,我创建了一个我全部使用的类,称为ConversionStrategy,该类考虑了源类型和目标类型,并选择了最理想(最灵活)的转换策略进行转换。
这个怎么样?
  byte i = 1; //or 0
  bool myBool = BitConverter.ToBoolean(new byte[] { i }, 0)
您Convert.ToBoolean(int value)不知道Parse方法是怎么回事:-)
代码无用:
        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");
        }
要回答这个问题将很困难。也许是因为Microsoft胸襟狭developers的开发人员有自己的原因?不要无视他们。只是说他们没有考虑将需要使用什么或如何使用它。我想不出以下扩展名对任何人都无效的原因。我的意思是布尔值是打开还是关闭,是或否。在我看来,它基本上是二进制的。Int,Double,Char,Long,Byte等的解析方法使用其Parse方法更为宽容。
但是,请考虑一下;您正在查看对象中是否存在值。对于以下内容也可以这样说...
string myvar = "empty"; //Or maybe = "NULL"
if (String.IsNullOrEmpty(myvar))
{
    //Should this be true?
}
无论如何,让我们简单一点。这是我使用扩展ToBoolean()方法为字符串创建方法的解决方案。
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;
}
BOOL: Zero is considered as false, nonzero values are considered as true.