Why does the statement
(int)"84"
throw an exception and
Convert.ToInt32("84")
does not throw an exception?
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.
First one is plain casting, which is changing a type of an object (more technically speaking, this is not casting, but type conversion). .NET indeed allows some conversions (like
inttolong, etc), but this particular one is disallowed. The reason I think this is disallowed is because only a small subset of strings can be actually converted tointand rules for doing so will be very cumbersome. Additionally, this might not play well with internationalization.The second is a method invocation, which actually parses string representation of an integer and constructs an
intout of it.