I can’t find an answer to the following question:
object o = 10; // Box
int i = (int)o; // Unbox
it’s clear, but the following isn’t clear
bool isInt = o is int; // Is the unbox here or not?
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.
No, that’s not unboxing – it’s just checking whether the type is correct. Don’t forget that there really is an object involved, with a type. Checking that type is basically the same operation regardless of whether the value is a boxed value type value or not. (There may be some optimizations feasible for value types or any sealed types, as there’s no inheritance to consider, but fundamentally it’s still checking the “type” part of an object header.)
One way to check that is to compile the code and look at the IL using ILASM:
So it uses
isinst– no unboxing is necessary.