Collection was modified; enumeration operation may not execute

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Collection was modified; enumeration operation may not execute

Message par Clippy »

Collection was modified; enumeration operation may not execute
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Collection was modified; enumeration operation may not execute

Message par Clippy »

What's likely happening is that `SignalData` is indirectly changing the subscribers dictionary under the hood during the loop and leading to that message. You can verify this by changing

```
foreach(Subscriber s in subscribers.Values)

```

To

```
foreach(Subscriber s in subscribers.Values.ToList())

```

If I'm right, the problem will disappear.

Calling `subscribers.Values.ToList()` copies the values of `subscribers.Values` to a separate list at the start of the `foreach`. Nothing else has access to this list (it doesn't even have a variable name!), so nothing can modify it inside the loop.
Répondre

Revenir à « .NET & C# »