Your split
method is passing around a reference to the same underlying baars
list. This can be demonstrated simply with:
List<int> a = new List<int>();
a.Add(1);
Console.WriteLine(a.Count); //1
List<int> b = a;
b.Add(2);
Console.WriteLine(b.Count); //2
Console.WriteLine(a.Count); //2
Console.WriteLine(Object.ReferenceEquals(a, b)); //true
Instead, you want to pass a copy of that list:
public Foo split(int amount)
{
int diff = this.amount - amount;
this.amount = amount;
List<Baar> baarsCopy = new List<Baar>(this.baars); //make a copy
return new Foo(diff, baarsCopy); //pass the copy
}
EDIT: Beyond that, I don't know if you want to make copies of the Baar
items inside that list as well or pass around/share references to the same Baar
instances. That's up to you and your application usage.