If convert. toboolean ("1") = true, the following error is returned:
This string is not recognized as a valid Boolean value.
Check the system. Convert. toboolean (string) method,CodeAs follows:
Public static bool toboolean (string value)
{
If (value = NULL)
{
Return false;
}
Return bool. parse (value );
}
Obviously related to system. boolean. parse (string), the Code is as follows:
Public static bool parse (string value)
{
If (value = NULL)
{
Throw new argumentnullexception ("value ");
}
If (string. Compare (value, bool. truestring, true, cultureinfo. invariantculture) = 0)
{
Return true;
}
If (string. Compare (value, bool. falsestring, true, cultureinfo. invariantculture )! = 0)
{
Value = value. Trim ();
If (string. Compare (value, bool. truestring, true, cultureinfo. invariantculture) = 0)
{
Return true;
}
If (string. Compare (value, bool. falsestring, true, cultureinfo. invariantculture )! = 0)
{
Throw new formatexception (environment. getresourcestring ("format_badboolean "));
}
}
Return false;
}
You just need to check out bool. truestring to find out the cause.
Bool. truestring = "true ";
Bool. falsestring = "false ";
No wonder an error is thrown.
Check the system. Convert. toboolean (int16) method. The Code is as follows:
Public static bool toboolean (short value)
{
Return (value! = 0 );
}
Therefore, you can rewrite it to: Convert. toboolean (convert. toint16 ("1 "))
It seems that the system. Convert. toboolean (string) method is not very well designed. What do you say?