[英]Does ldstr internally implement newobj?
As we all know strings are implicitly instantiated, meaning that we don't have to use new
in order to get a reference to an object of one. 众所周知,字符串是隐式实例化的,这意味着我们不必使用new
来获取对象的对象的引用。
Because of this it was always my belief that the framework is taking care of this, and hence I would get identical IL if I did something like this: 因此,我一直认为框架正在处理这个问题,因此如果我这样做,我会得到相同的IL:
String first = new String(new char[] {'a'});
string second = "a";
However it appears that the first line is done using newobj instance void [mscorlib]System.String::.ctor(char[])
and the second ldstr "a"
. 然而,似乎第一行是使用newobj instance void [mscorlib]System.String::.ctor(char[])
和第二行ldstr "a"
。
So in order to obtain a string reference, does ldstr
internally call newobj
and where can I see the specification / details to back this up? 那么为了获得一个字符串引用, ldstr
内部是否会调用newobj
,在哪里可以看到规范/详细信息来支持它?
ldstr
gives you the reference to the literal string as per the documentation (remember literal strings are interned per default, so they are only created once). ldstr
给你作为参考文本字符串每个文件 (记得文字串的每默认拘禁,所以他们只创造了一次)。 The first statement creates a regular instance of string
using the newobj
instruction as expected. 第一个语句按预期使用newobj
指令创建string
的常规实例。
string
simply follows tha basic guideline for reference object types, that's why on new
you see newobj
. string
只是遵循参考对象类型的基本准则,这就是为什么在new
你看到newobj
。
Infact if you try to write something like this, it will not generate newobj
: 事实上,如果你尝试写这样的东西,它将不会生成newobj
:
int a = new int();
a = 2;
Console.WriteLine(a);
The resulting IL will be 由此产生的IL将是
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldc.i4.2
IL_0003: stloc.0
IL_0004: ldloc.0
IL_0005: call System.Console.WriteLine
if you write just 如果你写的只是
int a = 2;
Console.WriteLine(a);
result IL will be 结果IL会
IL_0000: ldc.i4.2
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: call System.Console.WriteLine
No difference from allocation point of view (there is missed line naturally), cause we are talking about value type. 从分配的角度来看没有区别(自然有遗漏线),因为我们正在谈论价值类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.