什么是引擎盖下投 [英] What is a cast under the hood

查看:166
本文介绍了什么是引擎盖下投的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
   C#作为剧组VS经典投

我想知道.NET CLR的引擎盖下会发生什么,当我做这样的事情

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;

和如何在第二行不同于字符串myStr的= myObj.ToString()字符串myStr的= MyObj中的字符串;

and how the second line differs from string myStr = myObj.ToString() or string myStr = myObj as string;

环顾四周,我发现仿制药的答案,如编译器插入code有,但我不太满意......我要找的演员力学深undertanding ...哦,编译器插入code?给我看看!编译器optmizes的code?怎么样?当?

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获得尽可能接近金属,你可以!!!

Pls get as close to the metal as you can!!!

推荐答案

您可以使用的 IL Dissasembler 的,看看有什么正在制作由您code在一个较低的水平。如果您有Visual Studio安装在您的计算机上,你应该能够找到它只是在Windows搜索框中输入ILDASM。

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.

下面就是以下code中的IL是这样的:

Here's what the IL of the following code looks like:

object myObj = "abc";
string myStr = (string)myObj;    
string tostring = myObj.ToString();

这篇关于什么是引擎盖下投的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆