예. ildasm
또는 반사경을 사용하여 코드를 검사하여이를 확인할 수 있습니다.
static void Main(string[] args) {
string s = "A" + "B";
Console.WriteLine(s);
}
는 훨씬 더 흥미로운 무언가가있다
.method private hidebysig static void Main(string[] args) cil managed {
.entrypoint
// Code size 17 (0x11)
.maxstack 1
.locals init ([0] string s)
IL_0000: nop
IL_0001: ldstr "AB" // note that "A" + "B" is concatenated to "AB"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: call void [mscorlib]System.Console::WriteLine(string)
IL_000d: nop
IL_000e: br.s IL_0010
IL_0010: ret
} // end of method Program::Main
로 번역하지만, 그런 일이 관련되어있다. 어셈블리에서 문자열 리터럴을 사용하는 경우 CLR은 어셈블리에서 동일한 리터럴의 모든 인스턴스에 대해 하나의 개체 만 만듭니다. 따라서
:
static void Main(string[] args) {
string s = "A" + "B";
string t = "A" + "B";
Console.WriteLine(Object.ReferenceEquals(s, t)); // prints true!
}
콘솔에 "참"인쇄합니다! 이 최적화는 string interning입니다. Reflector에 따르면