声明&从内存消耗的角度调用函数

时间:2014-02-18 01:43:54

标签: c#-4.0 memory-management

考虑以下功能:

private void DoSomething(OtherObj obj) {
  //Use the obj.Id to do the job, that's the only
  //input this function really needs.
}

上面评论的函数说,使用obj.Id做它的工作,所以我们可以把它重写为

private void DoSomething(int otherObjectId) {
  //The same logic goes here...
}

我想知道从内存消耗的角度来看哪一个更好。请注意,OtherObj已经在内存中,因此在将该对象加载到内存等时没有附加任何字符串(在调用DoSomething函数之前必须加载其他内容,因此它只是在内存中)。

据我所知,obj的地址副本在第一个签名中传递给DoSomething函数,而第二个版本传递整数的副本。所以看起来两者之间没有区别......

请你解释一下这里发生了什么?哪一个是要走的路?为什么?

谢谢。

0 个答案:

没有答案