Convert.ToInt32, (), (int) can all be interpreted as converting a type to int, so what is the difference?
Convert.ToInt32 is more similar to , but in fact Convert.ToInt32 is called internally:
When the Convert.ToInt32 parameter is null, return 0;
When the parameter is null, an exception is thrown.
When the Convert.ToInt32 parameter is "", an exception is thrown;
When the parameter is "", an exception is thrown.
Convert.ToInt32 has many types that can be converted;
Only strings of numeric types can be converted.
It is similar to , but it will not generate an exception. It returns true if the conversion is successful, and it returns false if the conversion fails. The last parameter is the output value, and if the conversion fails, the output value is 0.
(int) The class cast conversion can only convert other numeric types to int type. It cannot convert strings. For example, the following example will fail:
Convert.ToInt32 is more similar to , but in fact Convert.ToInt32 is called internally:
When the Convert.ToInt32 parameter is null, return 0;
When the parameter is null, an exception is thrown.
When the Convert.ToInt32 parameter is "", an exception is thrown;
When the parameter is "", an exception is thrown.
Convert.ToInt32 has many types that can be converted;
Only strings of numeric types can be converted.
It is similar to , but it will not generate an exception. It returns true if the conversion is successful, and it returns false if the conversion fails. The last parameter is the output value, and if the conversion fails, the output value is 0.
(int) The class cast conversion can only convert other numeric types to int type. It cannot convert strings. For example, the following example will fail: