我正在将代码从 Java 重写为 C#。 我在 C# 中的克隆函数方面遇到问题。
代码在 Java:
public Tour(ArrayList tour)
{
this.tour = (ArrayList) tour.clone();
}
我的代码在 C#:
public Tour(List<City> tour)
{
//What I should do here?
}
我尝试了一些 C# 克隆技术,但没有结果。
编辑:
这个解决方案非常有效:
this.tour = new List<City>();
tour.ForEach((item) =>
{
this.tour.Add(new City(item));
});
您好!
最佳答案
The
java.util.ArrayList.clone()
returns a shallow copy of this ArrayList instance (i.e the elements themselves are not copied).
--Source
在 .NET 中做同样的事情 List<T>
你可以这样做:
var newList = oldList.ToList();
关于C# 中的 Java 克隆(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942047/