2016-05-14 2 views

答えて

4

string output = $"Hello World"; 

int integer = 5; 

string output2 = $"Hello World {integer}"; 

Console.WriteLine(output); 

Console.WriteLine(output2); 

私はコンパイルして、コンパイル時に、私は(ILSpy経由で)これを取得:

string value = "Hello World"; 
int num = 5; 
string value2 = string.Format("Hello World {0}", num); 
Console.WriteLine(value); 
Console.WriteLine(value2); 

だから、コンパイラがstring.Formatを使用しないように十分にスマートであると思われます最初のケースのために。

IL_0000: nop 
IL_0001: ldstr "Hello World" 
IL_0006: stloc.0 
IL_0007: ldc.i4.5 
IL_0008: stloc.1 
IL_0009: ldstr "Hello World {0}" 
IL_000e: ldloc.1 
IL_000f: box [mscorlib]System.Int32 
IL_0014: call string [mscorlib]System.String::Format(string, object) 
IL_0019: stloc.2 
IL_001a: ldloc.0 
IL_001b: call void [mscorlib]System.Console::WriteLine(string) 
IL_0020: nop 
IL_0021: ldloc.2 
IL_0022: call void [mscorlib]System.Console::WriteLine(string) 
IL_0027: nop 
IL_0028: ret 

それはここでもstring.Formatのみ第二ケースのために呼ばれていることは明らかである:

は完全を期すため、ここではILコードです。

+0

ありがとうございました。それが私が望んでいたことです。 – roydukkey

関連する問題