之间有什么区别:
public CustomObject MyMethod()
{
var myObject = new CustomObject();
return myObject
}
public int MyIntMethod()
{
var myInt = 1;
return myInt;
}
对此:
public CustomObject MyMethod()
{
return new CustomObject();
}
public int MyIntMethod()
{
return 1;
}
编译器做一些不同的事情吗?像是空检查或类似的东西?
最佳答案
编译器本身会讲UPDATED:(未经优化即可编译,经过优化后,编译后的代码是相同的,感谢您的评论。):
public CustomObject MyMethod()
{
var myObject = new CustomObject();
return myObject
}
var myObject = new CustomObject();
01933516 mov ecx,58413C4h
0193351B call 014D30F4
01933520 mov dword ptr [ebp-44h],eax
01933523 mov ecx,dword ptr [ebp-44h]
01933526 call 01930D10
0193352B mov eax,dword ptr [ebp-44h]
0193352E mov dword ptr [ebp-3Ch],eax
return myObject;
01933531 mov eax,dword ptr [ebp-3Ch]
01933534 mov dword ptr [ebp-40h],eax
01933537 nop
01933538 jmp 0193353A
VS
public CustomObject MyMethod()
{
return new CustomObject();
}
return new CustomObject();
00FD2E11 mov ecx,4E70F48h
00FD2E16 call 00A830F4
00FD2E1B mov dword ptr [ebp-40h],eax
00FD2E1E mov ecx,dword ptr [ebp-40h]
00FD2E21 call 00FD0D10
00FD2E26 mov eax,dword ptr [ebp-40h]
00FD2E29 mov dword ptr [ebp-3Ch],eax
00FD2E2C nop
00FD2E2D jmp 00FD2E2F
第一种方法有11条汇编指令,而第二种方法有9条汇编指令。还有更多的指针存储操作。