列表有没有更好的深度克隆方法?
Is there any better deep clone methods for list?
所以我需要一种深度克隆的方法。我想要一个卡片列表等于另一个卡片列表,但后来我也想修改其中一个克隆。
我做了一个复制列表的方法:
public List<Card> Copy(List<Card> cards)
{
List<Card> clone = new List<Card>();
foreach (var card in cards)
{
clone.Add(card);
}
return clone;
}
并像这样使用它:
_cards = new List<Card>();
_thrownCards = new List<Card>();
_cards = Copy(_thrownCards);
_thrownCards.Clear();
我没有使用 C# 的经验,但不知何故我的直觉告诉我我的复制方法可以更简单。没有其他方法可以深度复制列表吗?我尝试使用 MemberWiseClone,但它只是创建了对同一对象的引用,而不是克隆它(也许我误解了 MemberWiseClone 方法)。
有人知道如何简单地克隆列表对象吗?
这不是真正的深层复制,因为 Card
实例仍然相同,只是列表不同。你可以简单得多:
List<Card> cloneList = cards.ToList();
您还需要 "copy" Card
个实例的所有属性:
public List<Card> Copy(List<Card> cards)
{
List<Card> cloneList = new List<Card>();
foreach (var card in cards)
{
Card clone = new Card();
clone.Property1 = card.Property1;
// ... other properties
cloneList.Add(clone);
}
return cloneList;
}
您还可以提供一个工厂方法来创建给定 Card
实例的克隆:
public class Card
{
// ...
public Card GetDeepCopy()
{
Card deepCopy = new Card();
deepCopy.Property1 = this.Property1;
// ...
return deepCopy;
}
}
然后您将此逻辑封装在一个地方,您甚至可以在其中访问 private
成员(字段、属性、构造函数)。将上面Copy
方法中的一行改为:
cloneList.Add(card.GetDeepCopy());
怎么样
List<Card> _cards = _thrownCards.ConvertAll(Card => new Card(<card parameters here>));
要进行深层复制,您需要这样的东西:
public List<Card> Copy(List<Card> cards)
{
List<Card> clone = new List<Card>();
foreach (var card in cards)
{
clone.Add(new Card
{
property1 = card.property1;
property2 = card.property2; // and so on
});
}
return clone;
}
当然如果property1
和property2
也是引用类型的对象,那你就得更深入了。
不同于shallow复制,我们只复制引用
public List<Card> Copy(List<Card> cards) {
return cards.ToList();
}
在深度复制的情况下,我们必须克隆每个项目:
public List<Card> Copy(List<Card> cards) {
return cards
.Select(card => new Card() {
//TODO: put the right assignment here
Property1 = card.Property1,
...
PropertyN = card.PropertyN,
})
.ToList();
}
所以我需要一种深度克隆的方法。我想要一个卡片列表等于另一个卡片列表,但后来我也想修改其中一个克隆。
我做了一个复制列表的方法:
public List<Card> Copy(List<Card> cards)
{
List<Card> clone = new List<Card>();
foreach (var card in cards)
{
clone.Add(card);
}
return clone;
}
并像这样使用它:
_cards = new List<Card>();
_thrownCards = new List<Card>();
_cards = Copy(_thrownCards);
_thrownCards.Clear();
我没有使用 C# 的经验,但不知何故我的直觉告诉我我的复制方法可以更简单。没有其他方法可以深度复制列表吗?我尝试使用 MemberWiseClone,但它只是创建了对同一对象的引用,而不是克隆它(也许我误解了 MemberWiseClone 方法)。
有人知道如何简单地克隆列表对象吗?
这不是真正的深层复制,因为 Card
实例仍然相同,只是列表不同。你可以简单得多:
List<Card> cloneList = cards.ToList();
您还需要 "copy" Card
个实例的所有属性:
public List<Card> Copy(List<Card> cards)
{
List<Card> cloneList = new List<Card>();
foreach (var card in cards)
{
Card clone = new Card();
clone.Property1 = card.Property1;
// ... other properties
cloneList.Add(clone);
}
return cloneList;
}
您还可以提供一个工厂方法来创建给定 Card
实例的克隆:
public class Card
{
// ...
public Card GetDeepCopy()
{
Card deepCopy = new Card();
deepCopy.Property1 = this.Property1;
// ...
return deepCopy;
}
}
然后您将此逻辑封装在一个地方,您甚至可以在其中访问 private
成员(字段、属性、构造函数)。将上面Copy
方法中的一行改为:
cloneList.Add(card.GetDeepCopy());
怎么样
List<Card> _cards = _thrownCards.ConvertAll(Card => new Card(<card parameters here>));
要进行深层复制,您需要这样的东西:
public List<Card> Copy(List<Card> cards)
{
List<Card> clone = new List<Card>();
foreach (var card in cards)
{
clone.Add(new Card
{
property1 = card.property1;
property2 = card.property2; // and so on
});
}
return clone;
}
当然如果property1
和property2
也是引用类型的对象,那你就得更深入了。
不同于shallow复制,我们只复制引用
public List<Card> Copy(List<Card> cards) {
return cards.ToList();
}
在深度复制的情况下,我们必须克隆每个项目:
public List<Card> Copy(List<Card> cards) {
return cards
.Select(card => new Card() {
//TODO: put the right assignment here
Property1 = card.Property1,
...
PropertyN = card.PropertyN,
})
.ToList();
}