今天在调试接口时,遇到了以下问题:
运行这句 bool IsRoot = Convert.ToBoolean(dt.Rows[i]["IsRoot"].ToString()) ;结果抛出如下错误:该字符串未被识别为有效的布尔值。开始怀疑表里的数据问题,就换成了Convert.ToBoolean(“0”)试了试,还是报错,然后查了下MSDN于是就明白了。
方法的备注:若要成功执行转换,value 参数必须等于 Boolean.TrueString(值为 True 的常量)或 Boolean.FalseString(值为 False 的常量),否则必须为 null。 在对 value 与Boolean.TrueString 和 Boolean.FalseString 进行比较时,该方法忽略大小写以及前导和尾随空白。
using System;
2:
class BooleanConversion
4: {
void Main()
6: {
null);
8: ConvertToBoolean(String.Empty);
);
);
);
);
);
);
15: }
16:
value)
18: {
try
20: {
value,
value));
23: }
catch (FormatException)
25: {
value);
27: }
28: }
29: }
// The example displays the following output to the console:
// Converted '' to False.
// Unable to convert '' to a Boolean.
// Converted 'true' to True.
// Unable to convert 'TrueString' to a Boolean.
// Converted 'False' to False.
// Converted ' false ' to False.
// Unable to convert '-1' to a Boolean.
'0' to a Boolean.
该方法对参数只接受等于”True” 和“False”的值,只能换种方法写了。
说明:如果 value 为非零值,则为 true;否则为 false。
1:
int[] numbers = { Int32.MinValue, -201649, -68, 0, 612, 4038907,
3: Int32.MaxValue };
bool result;
5:
in numbers)
7: {
8: result = Convert.ToBoolean(number);
, number, result);
10: }
// The example displays the following output:
// -2,147,483,648 --> True
// -201,649 --> True
// -68 --> True
// 0 --> False
// 612 --> True
// 4,038,907 --> True
// 2,147,483,647 --> True
19:
有些方法自己还是不清楚用法啊,继续努力学习
http://www.cnblogs.com/xiaoyao2011/
欢迎任何形式的转载,但请务必注明出处。