【CSharp代码1】:
- string s0 = "";
- string s1 = null;
对应的反汇编代码:
- string s0 = "";
- 00000049 mov eax,dword ptr ds:[0232202Ch]
- 0000004f mov dword ptr [ebp-40h],eax
- string s1 = null;
- 00000052 xor edx,edx
- 00000054 mov dword ptr [ebp-44h],edx
上面的汇编代码中,””也是存在ds的堆栈中。
而s1=null的赋值则是采用 xor dex,dex,先对寄存器进行异或运行,相同为0,故寄存器dex被清0.
由此可以得出s0=“” 和 s1=null的本质区别:
string是引用类型。
s0=””,则s0得到了字符串””的引用,进行了分配内存空间的动作,只不过因为上层要求的是“”,故length()=0;
s1=null,则s1得到的引用为000为空,即是没有引用,根本没有分配内存空间!使用length()会报出异常。
实际编程过程中,a==null 和 a==””往往认为两者都表示无值。
【CSharp代码2】:
- string s2 = "aa"; //栈中
- string s3 = new string('a', 2);
对应的反汇编代码:
- string s2 = "aa";
- 00000057 mov eax,dword ptr ds:[02322088h]
- 0000005d mov dword ptr [ebp-48h],eax
- string s3 = new string('a', 2);
- 00000060 push 2
- 00000062 mov edx,61h
- 00000067 xor ecx,ecx
- 00000069 call 759CF598
- 0000006e mov dword ptr [ebp-54h],eax
- 00000071 mov eax,dword ptr [ebp-54h]
- 00000074 mov dword ptr [ebp-4Ch],eax
上面的两个语句是有本质上的区别的:
s2 = "aa";的操作过程是先在栈中写入”aa”,再让s2指向它;
string s3 = new string('a', 2);则是堆上创建的一个“aa”的字符串对象。s3就是对象名,代表着对象的首地址。
参考文献: