As already stated what you are looking to do is not possible. However, an alternative solution would be to simply maintain a list of items marked for deletion and then remove these afterwords. I would also opt for a foreach
rather than a while
loop, less code e.g.
var removeList = new List<decimal>();
foreach (var item in myDictionary)
{
// have a condition which indicates which items are to be removed
if (item.Key > 1)
{
removeList.Add(item.Key);
}
}
Or if you are simply trying to retrieve items for deletion, use LINQ
var removeList = myDictionary.Where(pair => pair.Key > 1).Select(k => k.Key).ToList();
Then just remove them from the list.
// remove from the main collection
foreach (var key in removeList)
{
myDictionary.Remove(key);
}