Doing something like this
public Multime<T> Difference(Multime<T> list)
{
Multime<T> diff = new Multime<T>();
for (int i = 0; i < list.Multisets.Count() - 1; i++)
{
foreach (T el in list.multiSets[i].Multimea)
{
if (!(list.multiSets[i + 1].Multimea.Exists(element => EqualityComparer<T>.Default.Equals(element, el))))
diff.Multimea.Add(el);
}
list.multiSets[i + 1] = diff;
if (i < list.multiSets.Count() - 1)
diff.Multimea.RemoveAll(item => EqualityComparer<T>.Default.Equals(item, item));
}
...
after diff.Multimea.RemoveAll(item => EqualityComparer<T>.Default.Equals(item, item));
the list.multiSets[i + 1] is cleared too. How can i resolve this, so that they wont be connected this way, I dont want list.multiSets[i + 1] to be cleared.
public Multime<T> this[int index]
{
get { return this.multiSets[index]; }
set { this.multiSets[index] = value; }
}
this is the indexer if it helps 🙂
You’ll have to fill it with a copy:
Your
diffandlist.multiSets[x]values are reference types. This means that you original assignment only copies a reference to a list, not the list itself.ToList()is a compact way of making a clone of the list.