【发布时间】:2011-12-09 17:50:39
【问题描述】:
最近,我开始从事一个项目,其中许多引用参数通过引用传递。我查看了很多代码,其中大多数方法可能会丢失ref 参数。尽管代码运行良好,但我想知道通过双重取消引用每个参数而不是仅按值传递引用可能会对性能造成什么样的影响。
例如这段代码:
public void EmptyList(ref List<string> test)
{
test.Clear();
}
VS 这个:
public void EmptyList(List<string> test)
{
test.Clear();
}
编辑
在阅读了 Nicholas Carey 所说的之后,我用上面的代码尝试了同样的事情。这是生成的程序集:
EmptyList(ref List<string> test)
00000028 mov eax,dword ptr [ebp-40h]
0000002b mov ecx,dword ptr [eax]
0000002d cmp dword ptr [ecx],ecx
0000002f call 719210C4
EmptyList(List<string> test)
00000028 mov ecx,dword ptr [ebp-40h]
0000002b cmp dword ptr [ecx],ecx
0000002d call 71920FD4
【问题讨论】:
-
你可能不会注意到。
标签: c# pass-by-reference pass-by-value