我刚刚在我的一个类中创建了以下方法
public static bool Assimilate(this List<Card> first, List<Card> second)
{
// Trivial
if (first.Count == 0 || second.Count == 0)
{
return false;
}
// Sort the lists, so I can do a binarySearch
first.Sort();
second.Sort();
// Copia only the new elements
int index;
for (int i = 0; i < second.Count; i++)
{
index = first.BinarySearch(second[i]);
if (index < 0)
{
first.Insert(~index, second[i]);
}
}
// Edit
second = null;
return true;
}
我的一个朋友在查看我的代码时说,我不应该创建“扩展 List 类”的方法,因为这违反了开放/封闭原则。如果我想扩展 List 类,我应该创建一个继承自 List 的新类,并在该新类中实现我的“合并”方法。他是对的吗?扩展 List 类违反了 Open/Closed 原则?