Problem with removing an item from a list
Hi,
I found a surprising behaviour of List.Remove that I was not expecting in the following code.
The first 4 lines of code work as expected.
Then I try to remove an item bien.info of type Immoinfo, which is the type of the list biensAVendre.
The commented line does not work (the item stays in the list)
However, the foreach loop after the commented line works fine.
DetailsBien is a type which contains some info and a type DetailsBien.info
I crosscheck that the data in bien.info is exactly the same as the item in the list biensAVendre that I want to remove (I never modify this list anyway).
I don't understand why the commented line does not work. Any idea ?
public void MettreSousCompromis(DetailsBien bien)
{
bien.SemainesAttenteFinancement = 8 + UnityEngine.Random.Range(1, 6);
bien.SousCompromis = true;
biensSousCompromis.Add(bien);
detailBienAVendre.Remove(bien);
// biensAVendre.Remove(bien.info); ==> does not work for some reason I don't understand.
foreach (var item in biensAVendre)
{
if (item.UID == bien.info.UID)
{
biensAVendre.Remove(item);
break;
}
}
}
Comment