Asp tutorial. net c # int type conversion
Int I =-1; bool B = int. tryparse (null, out I );
After the execution is complete, B is equal to false, I is equal to 0, rather than-1. Remember.
Int I =-1; bool B = int. tryparse ("123", out I );
After the execution is completed, B is equal to true, and I is equal to 123;
1. (int) is a type conversion. When the type is long, float, double, decimal, implicit conversion can be used, however, Explicit conversions are required from the long type to the int type. Otherwise, a compilation error occurs. 2. int. parse () is a type of capacity conversion. It indicates converting the numeric content string to the int type.
If the string is null, an argumentnullexception exception is thrown;
If the string content is not a number, a formatexception is thrown;
Overflowexception is thrown if the number indicated by the string content exceeds the range indicated by the int type;
3. int. tryparse is similar to int. parse, but it does not produce exceptions,
If the conversion is successful, true is returned. If the conversion fails, false is returned.
The last parameter is the output value. If the conversion fails, the output value is 0. If the conversion is successful, the output value is the converted int value.
4. convert. toint32 () is a type-tolerant conversion, but it is not limited to converting a string to the int type, or it can be another type of parameter;
Comparison: If the convert. toint32 parameter is null, 0 is returned; if the int. parse parameter is null, an exception is thrown. If the convert. toint32 parameter is "", an exception is thrown. If the int. parse parameter is "", an exception is thrown. Convert. toint32 can be converted to many types. int. parse can only be converted to numeric strings.
Convert. toint32 to force conversion, but it is found that when the string value is decimal, scientific notation cannot be converted. This is not the case if the string is converted to detimal before toint32.
Public static int do (string value)
{
If (string. isnullorempty (value) return 0;
Decimal result = 0;
Try {decimal. tryparse (value, out result );}
Catch {}
Return system. convert. toint32 (result );
}
All my conversion class code
Namespace cs. convert
{
Public class toint
{
Public static int do (string value)
{
If (string. isnullorempty (value) return 0;
Decimal result = 0;
Try {decimal. tryparse (value, out result );}
Catch {}
Return system. convert. toint32 (result );
}
Public static int do (decimal value)
{
Return system. convert. toint32 (value );
}
Public static int do (float value)
{
Return system. convert. toint32 (value );
}
Public static int do (double value)
{
Return system. convert. toint32 (value );
}
Public static int do (int value)
{
Return value;
}
Public static int do (object value)
{
If (null = value | string. isnullorempty (value. tostring () return 0;
Decimal result = 0;
Try {decimal. tryparse (value. tostring (), out result );}
Catch {}
Return system. convert. toint32 (result );
}
Public static int do (dbnull value)
{
Return 0;
}
}
}