Java C# 中的克隆()

Java clone() in C#

我正在将代码从 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));
});

您好!

您应该在 City class 上实施 ICloneable

这样你就可以做到:

public List<City> Tour {get; private set;}

public Tour(List<City> tour)
{
    this.Tour = new List<City>();
    foreach (var city in tour)
      this.tour.Add((City)city.Clone());    
}

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();