My question is is it safe for enumerator to remove item from SortedList?
SortedList<decimal, string> myDictionary;
// omitted code
IEnumerator<decimal, string> enum = myDictionary.GetEnumerator();
while(enum.MoveNext)
{
// is it ok to remove here?
myDictionary.Remove(enum.Current.Key);
}
This will throw an exception – you cannot modify a collection while iterating over it.
If you think about it a little, you will understand why. If adding or removing from the collection was allowed, you would no longer be iterating over the same collection – you either have too many (adding) or not enough items (removing).