Does foreach() iterate by reference?

前端 未结 10 1244
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 23:37

Consider this:

List obj_list = get_the_list();
foreach( MyClass obj in obj_list )
{
    obj.property = 42;
}

Is obj

10条回答
  •  自闭症患者
    2020-12-04 23:59

    obj is a reference to an item inside the List, hence if you change it's value it will persist. Now what you should be concerned about is whether or not get_the_list(); is making a deep copy of the List or returning the same instance.

提交回复
热议问题