Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9276845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:57:01+00:00 2026-06-18T16:57:01+00:00

Can anyone help me understand why the output of the two for loops below

  • 0

Can anyone help me understand why the output of the two for loops below produce different outputs?

They to my eye are the same, however the original Dictionary object’s for loop outputs all 9 of its members, but the Queue objects for loop outputs just the first 5.

void test()
{
        Dictionary<int, string> d = new Dictionary<int, string>();

        d.Add(0, "http://example.com/1.html");
        d.Add(1, "http://example.com/2.html");
        d.Add(2, "http://example.com/3.html");
        d.Add(3, "http://example.com/4.html");
        d.Add(4, "http://example.com/5.html");
        d.Add(5, "http://example.com/6.html");
        d.Add(6, "http://example.com/7.html");
        d.Add(7, "http://example.com/8.html");
        d.Add(8, "http://example.com/9.html");

        Queue<KeyValuePair<int, string>> requestQueue = new Queue<KeyValuePair<int, string>>();

        // build queue
        foreach (KeyValuePair<int, string> dictionaryListItem in d)
        {
            requestQueue.Enqueue(dictionaryListItem);
            Console.WriteLine(dictionaryListItem.Value);
        }

        Console.WriteLine("          ");

        for (int i = 0; i < requestQueue.Count; i++)
        {
            Console.WriteLine(requestQueue.Peek().Value);
            requestQueue.Dequeue();
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:57:03+00:00Added an answer on June 18, 2026 at 4:57 pm

    You need to save count before the loop:

    var count = requestQueue.Count;
    for (int i = 0; i < count; i++)
    {
        Console.WriteLine(requestQueue.Peek().Value);
        requestQueue.Dequeue();
    }
    

    The reason is that it is evaluated at each iteration of the for loop:

    At the start of the first iteration, requestQueue.Count is 9, i is 0.
    2nd iteration: requestQueue.Count is 8, i is 1.
    3rd iteration: requestQueue.Count is 7, i is 2.
    4th iteration: requestQueue.Count is 6, i is 3.
    5th iteration: requestQueue.Count is 5, i is 4.
    6th iteration: requestQueue.Count is 4, i is 5. –> Exit loop.

    Note: The Count of the queue is reduced with each iteration because Queue.Dequeue removes the first item in the queue and returns it to the caller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help me understand why the piece of code below returns no results
Can anyone help me understand what is going on here? Jenkins has been working
Hey can anyone help me understand how to do an edit method for my
Can anyone please help me to understand how should i access json data in
I wonder if anyone can help me to understand where I could be going
Can anyone please help me to understand me how to use array - passed
Can anyone help me understand why program requires me to input my selection twice
Can anyone help me understand how the DataTables $.fn.dataTableExt.afnFiltering.push method works? I know that
Can anyone help - this is driving me mad. I am calling a mysql
Can Anyone help me why x2 prints zero. I guess because of floating point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.