Possible Duplicate:
Direct casting vs 'as' operator?
Can someone explain the difference to me and which one is better to use? I know in some situations I can only use one or the other.
(int)value
value as int
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.
The latter is invalid. You can use
if you need to “convert if possible”. That’s slower than
though. That’s what you should probably use if you’re not confident that
valueis actually anint. If, however, your code is such that ifvalueisn’t anint, that represents a bug, then just cast unconditionally:If that fails, it will throw an exception – which is generally appropriate for a bug.