问题描述
大家都知道串隐式实例化,这意味着我们不必使用新
为了得到一个引用一个对象。
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.
由于这一点,一直是我的信念,即框架照顾到这一点,所以我会得到相同的IL,如果我做了这样的事情的:
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:
String first = new String(new char[] {'a'});
string second = "a";
不过似乎用做第一行 newobj实例无效[mscorlib程序] System.String ::。ctor的(的char [])
第二 ldstr一个
。
因此,为了获得一个字符串引用,确实 ldstr
内部调用 newobj
和我在哪里可以看到规格/细节来支持这一行动?
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
为您提供了参考文本字符串作为的(记住文字字符串是按默认的实习,让他们只创建一次库/ system.reflection.emit.op codes.ldstr.aspx> )。第一条语句创建字符串
使用 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). The first statement creates a regular instance of string
using the newobj
instruction as expected.
这篇关于难道ldstr内部实现newobj?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!