【发布时间】:2010-10-11 21:46:59
【问题描述】:
我见过这样的方法:
public void Foo(List<string> list)
{
list.Add("Bar");
}
这是修改方法中的参数的好习惯吗?
这样不是更好吗?
public List<string> Foo(List<string> list)
{
// Edit
List<string> newlist = new List<string>(list);
newlist.Add("Bar");
return newlist;
}
感觉第一个例子有意想不到的副作用。
【问题讨论】:
-
我想你的意思是 List
newlist = new List (list); -
谢谢。我已经更新了。
标签: c# side-effects