I could write the following to convert an object to an integer.
Convert.ToInt32(myObject);
But I could also write
Int.Parse(myObject.ToString());
- Is there any difference?
- Which one should I be using?
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes,
Int32.parse(myObject.ToString());takes a detour to string, that will usually work but it is unnecessary and it might fail or give a different result.In general,
Convert.ToInt32(myObject);But it depends on what type of data you want to convert.
If
myObject = '1';, do you want1or49?If
myObject = false;, do you want0or an exception ?etc