The following code's foreach statement throws an InvalidOperationException saying "Collection has been modified. Enumeration operation cannot execute." I don't see how this is possible, seeing as colors can never be modified after it's initialization.
Dictionary<Color, int> colorDictionary = new Dictionary<Color, int>();
//Put stuff in colorDictionary...
int currentBest = 257;
Color replaceColor = Color.Empty;
Dictionary<Color, int>.KeyCollection colors = colorDictionary.Keys;
foreach (Color c in colors)
{
if (colorDictionary[c] == 0)
{
continue;
}
if (ColorDistance(color, c) < currentBest)
{
replaceColor = c;
colorDictionary[c]--;
}
}