Possible Duplicate:
C# “as” cast vs classic cast
I want to know what happens under the hood of the .Net CLR when I do something like
object myObj = "abc"; string myStr = (string)myObj;
and how the second line differs from string myStr = myObj.ToString() or string myStr = myObj as string;
looking around I found generics answers such as “the compiler inserts code there” but I’m not satisfied… I’m looking for deep undertanding of the cast mechanics… Oh the compiler inserts code? Show me! The compiler optmizes the code? How? When?
Pls get as close to the metal as you can!!!
You can use IL Dissasembler to see what is being produced by your code on a lower level. If you have Visual Studio installed on your machine, you should be able to find it just by typing “ildasm” in the windows search box.
Here’s what the IL of the following code looks like: