The (int)variável
is a cast , which in the case of your question will not work for String (string), giving this error message:
CS0030 Can not convert type 'string' to 'double'
In case you can use Convert
, but nowadays we use int.Parse
or better int.TryParse
.
Actually, Convert
is not used very much these days because of some conversions problems ...
Example:
ref site and all credits to Convert.ToInt32 vs Int32.Parse - Source Code
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "1234567891234567891123456789123456789";
Convert
result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException
Parse
result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException
Recommendation:
Always use methods related to type conversions
int
, use int.Parse
or TryParse
, and so on, bringing even greater clarity to your code. Convert
has some methods that are used at some point in your code, but the frequency is low!