我有一个对象
public class Parent{
public List<string> docs
}
Parent p = new Parent();
p.docs.Add("First");
p.docs.Add("Second");
p.docs.Add("Third");
p.docs.Add("Fourth");
p.docs.Add("Fifth");
process(p); //some method which take Parent
您是否可以仅通过一个孩子/一组孩子将对象p
传递给process
方法,并为每个孩子组调用它。我发现有点难以解释,但本质上是我想做的
process(p); Parent
->孩子First,Second
process(p); Parent
->孩子Third, Fourth
process(p); Parent
->孩子Fifth
答案 0 :(得分:1)
嗨,尝试这样的事情
public class Parent{
public List<string> docs
}
Parent p = new Parent();
p.docs.Add("First");
p.docs.Add("Second");
p.docs.Add("Third");
p.docs.Add("Fourth");
p.docs.Add("Fifth");
process(new Parent{docs =p.docs.Take(5)});
before that i would recomend you to check memberwiseclone (Shallow Copy,Deep Copy)