Convert.ToBoolean(“0”) 的错误用法

     今天在调试接口时,遇到了以下问题:

 

     运行这句 bool IsRoot = Convert.ToBoolean(dt.Rows[i]["IsRoot"].ToString()) ;结果抛出如下错误:该字符串未被识别为有效的布尔值。开始怀疑表里的数据问题,就换成了Convert.ToBoolean(“0”)试了试,还是报错,然后查了下MSDN于是就明白了。

    方法的备注:若要成功执行转换,value 参数必须等于 Boolean.TrueString(值为 True 的常量)或 Boolean.FalseString(值为 False 的常量),否则必须为 null。 在对 value 与Boolean.TrueStringBoolean.FalseString 进行比较时,该方法忽略大小写以及前导和尾随空白。

 

   1: using System;
   2:  
   3: public class BooleanConversion
   4: {
   5:    public static void Main()
   6:    {
   7:       ConvertToBoolean(null);
   8:       ConvertToBoolean(String.Empty);
   9:       ConvertToBoolean("true");
  10:       ConvertToBoolean("TrueString");
  11:       ConvertToBoolean("False");
  12:       ConvertToBoolean("    false    ");
  13:       ConvertToBoolean("-1");
  14:       ConvertToBoolean("0");
  15:    }
  16:  
  17:    private static void ConvertToBoolean(string value)
  18:    {
  19:       try
  20:       {
  21:          Console.WriteLine("Converted '{0}' to {1}.", value,  
  22:                            Convert.ToBoolean(value));
  23:       }
  24:       catch (FormatException)
  25:       {
  26:          Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
  27:       }
  28:    }
  29: }
  30: // The example displays the following output to the console:
  31: //       Converted '' to False.
  32: //       Unable to convert '' to a Boolean.
  33: //       Converted 'true' to True.
  34: //       Unable to convert 'TrueString' to a Boolean.
  35: //       Converted 'False' to False.
  36: //       Converted '    false    ' to False.
  37: //       Unable to convert '-1' to a Boolean.
  38: //       Unable to convert '0' to a Boolean.

     该方法对参数只接受等于”True” 和“False”的值,只能换种方法写了。

Convert.ToBoolean  (Int32)

     说明:如果 value 为非零值,则为 true;否则为 false。

   1:  
   2: int[] numbers = { Int32.MinValue, -201649, -68, 0, 612, 4038907, 
   3:                   Int32.MaxValue };
   4: bool result;
   5:  
   6: foreach (int number in numbers)
   7: {
   8:    result = Convert.ToBoolean(number);                                 
   9:    Console.WriteLine("{0,-15:N0}  -->  {1}", number, result);
  10: }
  11: // The example displays the following output:
  12: //       -2,147,483,648   -->  True
  13: //       -201,649         -->  True
  14: //       -68              -->  True
  15: //       0                -->  False
  16: //       612              -->  True
  17: //       4,038,907        -->  True
  18: //       2,147,483,647    -->  True
  19:  
 
    
 
    
 
    
有些方法自己还是不清楚用法啊,继续努力学习


  欢迎扫描此二维码关注web馆公众号
  作者:web馆
  出处:http://www.cnblogs.com/xiaoyao2011/
  欢迎任何形式的转载,但请务必注明出处。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值