クラスの1つで次のメソッドを作成しました
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 の原則に違反しますか?