交换字符串会导致临时字符串被分配吗?

时间:2016-10-18 07:51:08

标签: c# arrays string

我们说我有一个字符串数组,如下所示:

string[] inputs = 
{
    "abc",
    "abb",
    "aba" // ....
};

我在这个数组中交换了两个元素,如下所示:

string tmp = inputs[i];
inputs[i] = inputs[j];
inputs[j] = tmp;

交换时是否会创建临时字符串

1 个答案:

答案 0 :(得分:0)

以下是一些示例代码:

public class Hello1
{
   public static void Main()
   {
        string[] inputs = {"abc","abb","aba"};
        Swap(inputs,1,3);
        Copy_Alloc(inputs[2]);
   }
   private static void Swap(const string[] inputs,int i,int j)
   {
        string tmp = inputs[i];
        inputs[i] = inputs[j];
        inputs[j] = tmp;
   }
   private static void Copy_Alloc(string inputs)
   {
        string copy = string.Copy(inputs);
        string another = "abc";
        string combined = copy + "cba";
   }
}

和它的IL(得到ILSpy;只显示相关部分):

.method public hidebysig static 
    void Main () cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 53 (0x35)
    .maxstack 3
    .entrypoint
    .locals init (
        [0] string[],
        [1] string[]
    )

    IL_0000: nop
    IL_0001: ldc.i4.3
    IL_0002: newarr [mscorlib]System.String
    IL_0007: stloc.1
    IL_0008: ldloc.1
    IL_0009: ldc.i4.0
    IL_000a: ldstr "abc"
    IL_000f: stelem.ref
    IL_0010: ldloc.1
    IL_0011: ldc.i4.1
    IL_0012: ldstr "abb"
    IL_0017: stelem.ref
    IL_0018: ldloc.1
    IL_0019: ldc.i4.2
    IL_001a: ldstr "aba"
    IL_001f: stelem.ref
    IL_0020: ldloc.1
    IL_0021: stloc.0
    IL_0022: ldloc.0
    IL_0023: ldc.i4.1
    IL_0024: ldc.i4.3
    IL_0025: call void Hello1::Swap(string[], int32, int32)
    IL_002a: nop
    IL_002b: ldloc.0
    IL_002c: ldc.i4.2
    IL_002d: ldelem.ref
    IL_002e: call void Hello1::Copy_Alloc(string)
    IL_0033: nop
    IL_0034: ret
} // end of method Hello1::Main

.method private hidebysig static 
    void Swap (
        string[] inputs,
        int32 i,
        int32 j
    ) cil managed 
{
    // Method begins at RVA 0x2094
    // Code size 16 (0x10)
    .maxstack 4
    .locals init (
        [0] string
    )

    IL_0000: nop
    IL_0001: ldarg.0
    IL_0002: ldarg.1
    IL_0003: ldelem.ref
    IL_0004: stloc.0
    IL_0005: ldarg.0
    IL_0006: ldarg.1
    IL_0007: ldarg.0
    IL_0008: ldarg.2
    IL_0009: ldelem.ref
    IL_000a: stelem.ref
    IL_000b: ldarg.0
    IL_000c: ldarg.2
    IL_000d: ldloc.0
    IL_000e: stelem.ref
    IL_000f: ret
} // end of method Hello1::Swap

.method private hidebysig static 
    void Copy_Alloc (
        string inputs
    ) cil managed 
{
    // Method begins at RVA 0x20b0
    // Code size 27 (0x1b)
    .maxstack 2
    .locals init (
        [0] string,
        [1] string,
        [2] string
    )

    IL_0000: nop
    IL_0001: ldarg.0
    IL_0002: call string [mscorlib]System.String::Copy(string)
    IL_0007: stloc.0
    IL_0008: ldstr "abc"
    IL_000d: stloc.1
    IL_000e: ldloc.0
    IL_000f: ldstr "cba"
    IL_0014: call string [mscorlib]System.String::Concat(string, string)
    IL_0019: stloc.2
    IL_001a: ret
} // end of method Hello1::Copy_Alloc

正如您所看到的,在Swap()中,只传递对现有对象的引用(您知道字符串是引用类型)。为了进行比较,Copy_Alloc()显示了新的分配,复制和转换的样子。