我从C/C++转到C#领域,对细节还有点模糊。据我所知,类默认情况下是通过引用传递的,但像List<string>这样的对象呢?比如:
void DoStuff(List<string> strs)
{
//do stuff with the list of strings
}
以及其他地方
List<string> sl = new List<string>();
//next fill list in a loop etc. and then do stuff with it:
DoStuff(sl);
在这种情况下,sl是按引用传递还是会被复制一份, 因此我需要重新定义worker函数,例如
void DoStuff(ref List<string> strs)来实际操作sl本身而不是它的一个副本?
class
通过引用传递,struct
通过值传递。 - Dialecticusref
参数,与C++和CS中一般所称的“按引用传递”基本相同。 - user395760