String s = "abc";创建了几个字符串对象?
数据类型分为两类,值类型和引用类型。内存分为栈空间和堆空间。 栈空间存放值类型数据和堆空间的引用地址,堆空间则存放引用类型的数据。 string 型是特殊的引用类型,所以这里创建了一个string型的对象,在名为str的栈空间中存放了真正存放“abc”的堆空间地址。
static void Main()
{
string S = "Hello";
string S2 = "Hello";
Console.WriteLine(S == S2 );
string S3 = new string('H',5);
string S4 = new string('H', 5);
Console.WriteLine(S3 == S4);
string S5 =new string(new char[] { 'H', 'e', 'l', 'l', 'o' });
string S6 = new string(new char[] { 'H', 'e', 'l', 'l', 'o' });
Console.WriteLine(S5 == S6);
Console.WriteLine("*************");
Console.WriteLine(S == S6);
上面的执行全打印true!到底
new string('H',5);有没有创建对象啊?
看IL:下面蓝色的部分是说明有声明string的,这只是在栈中(看开头介绍),他会指向一个堆中的东西。如果最终的结果一样,他们最终会指向同一个。可惜在这儿,通过IL也分析不出来,再去string的构造函数中看,看不到构造函数体了。这全是推测了。
void Main () cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x210c
// Code size 184 (0xb8)
.maxstack 3
.entrypoint
.locals init (
[0] string,
[1] string,
[2] string,
[3] string,
[4] string,
[5] string
)
// sequence point: (line 16, col 9) to (line 16, col 10) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0000: nop
// sequence point: (line 18, col 13) to (line 18, col 32) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0001: ldstr "Hello"
IL_0006: stloc.0
// sequence point: (line 19, col 13) to (line 19, col 33) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0007: ldstr "Hello"
IL_000c: stloc.1
// sequence point: (line 20, col 13) to (line 20, col 41) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_000d: ldloc.0
IL_000e: ldloc.1
IL_000f: call bool [mscorlib]System.String::op_Equality(string, string)
IL_0014: call void [mscorlib]System.Console::WriteLine(bool)
IL_0019: nop
// sequence point: (line 22, col 13) to (line 22, col 43) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_001a: ldc.i4.s 72
IL_001c: ldc.i4.5
IL_001d: newobj instance void [mscorlib]System.String::.ctor(char, int32)
IL_0022: stloc.2
// sequence point: (line 23, col 13) to (line 23, col 44) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0023: ldc.i4.s 72
IL_0025: ldc.i4.5
IL_0026: newobj instance void [mscorlib]System.String::.ctor(char, int32)
IL_002b: stloc.3
// sequence point: (line 24, col 1) to (line 24, col 29) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_002c: ldloc.2
IL_002d: ldloc.3
IL_002e: call bool [mscorlib]System.String::op_Equality(string, string)
IL_0033: call void [mscorlib]System.Console::WriteLine(bool)
IL_0038: nop
// sequence point: (line 27, col 13) to (line 27, col 76) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0039: ldc.i4.5
IL_003a: newarr [mscorlib]System.Char
IL_003f: dup
IL_0040: ldtoken field valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=10' '<PrivateImplementationDetails>'::D2EFCBBA102ED3339947E85F4141EB08926E40E9
IL_0045: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
IL_004a: newobj instance void [mscorlib]System.String::.ctor(char[])
IL_004f: stloc.s 4
// sequence point: (line 28, col 13) to (line 28, col 76) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0051: ldc.i4.5
IL_0052: newarr [mscorlib]System.Char
IL_0057: dup
IL_0058: ldtoken field valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=10' '<PrivateImplementationDetails>'::D2EFCBBA102ED3339947E85F4141EB08926E40E9
IL_005d: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
IL_0062: newobj instance void [mscorlib]System.String::.ctor(char[])
IL_0067: stloc.s 5
// sequence point: (line 30, col 13) to (line 30, col 41) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0069: ldloc.s 4
IL_006b: ldloc.s 5
IL_006d: call bool [mscorlib]System.String::op_Equality(string, string)
IL_0072: call void [mscorlib]System.Console::WriteLine(bool)
IL_0077: nop
// sequence point: (line 32, col 13) to (line 32, col 48) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0078: ldstr "*************"
IL_007d: call void [mscorlib]System.Console::WriteLine(string)
IL_0082: nop
// sequence point: (line 33, col 13) to (line 33, col 40) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0083: ldloc.0
IL_0084: ldloc.s 5
IL_0086: call bool [mscorlib]System.String::op_Equality(string, string)
IL_008b: call void [mscorlib]System.Console::WriteLine(bool)
IL_0090: nop
// sequence point: (line 36, col 13) to (line 36, col 36) in c:\users\administrator\source\repos\StringTest\StringTest\Program.cs
IL_0091: ldloc.0
IL_0092: ldloc.1
IL_0093: ldloc.2
IL_0094: call string [mscorlib]System.String::Concat(string, string, string)
IL_0099: call void [mscorlib]System.Console::Write(string)
IL_009e: nop