Collection was modified; enumeration operation may not execute
<p>I have this code that removes a player if the player is not alive, but I figured the problem is to do with the <code>foreach</code> loop. I've seen solutions involving making new lists, but I cannot see how I can apply it to my code. Can anyone please shed some light?</p>

<pre><code>private Dictionary&lt;int, Player&gt; numPlayers = new Dictionary&lt;int, Player&gt;();

private void CheckPlayers()
{
    foreach (Player player in numPlayers.Values)
    {
        if (!player.isAlive)
        {
            canvas.Children.Remove(player.hand);
            numPlayers.Remove(player.id); // breaks here
        }
    }
}
</code></pre>
