Jump to content
  • Advertisement
Sign in to follow this  
noatom

c# question

This topic is 2126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

            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?

Share this post


Link to post
Share on other sites
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);
    }
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!