c# question

Started by
0 comments, last by lwm 10 years, 7 months ago

            Queue<string> RemoveKeys = new Queue<string>();

            foreach (string thisKey in rotatingPieces.Keys)
            {
                
                if (1)
                    RemoveKeys.Enqueue(thisKey.ToString());
            }

            while (RemoveKeys.Count > 0)
                rotatingPieces.Remove(RemoveKeys.Dequeue());

my question is,when the remove is called,does it remove keys from RemoveKeys AND rotatingPieces or just RemoveKeys?

Advertisement

Queue.Dequeue removes and returns the head of the queue. For each returned key, the pair with this key is removed from rotatingPieces. So yes, both the queue and the dictionary will be empty afterwards.

Since filtering a dictionary is rather common, I use an extension method:


public static void RemoveWhere<K, V>(this Dictionary<K, V> dict, Func<KeyValuePair<K, V>, bool> predicate)
{
    var toRemove = dict.Where(predicate).ToList();
    foreach (var item in toRemove)
    {
        dict.Remove(item.Key);
    }
}

current project: Roa

This topic is closed to new replies.

Advertisement