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 7042507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:10:37+00:00 2026-05-28T02:10:37+00:00

The first foreach method gets several errors. I can’t figure out why and it

  • 0

The first foreach method gets several errors. I can’t figure out why and it seems like it should work…

foreach – invalid token ‘foreach’ in class, struct, or interface member declaration.

This prints out 1 2 3 4 1 2 3 4 1 2 3 4.

the 2nd foreach method. How does this work? I think it just iterates through each number one at a time in order. The confusion comes in where it is the same code, but stack instead of queue. The 2nd foreach prints out 4 3 2 1. Why is this?

namespace Cards
{
    class Class1
    {
        Queue numbers = new Queue();

        foreach (int number in new int[4]{1,2,3,4})
        {
             numbers.Enqueue(number);
            Console.WriteLine(number + " has joined the queue");
        }
        foreach (int number in numbers)
        {
             Console.WriteLine(number);
        }
        while(numbers.Count > 0)
        {
            int number = (int)numbers.Dequeue();
            Console.WriteLine(number + " has left the queue");
        }
    }

}
  • 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-05-28T02:10:37+00:00Added an answer on May 28, 2026 at 2:10 am

    that code needs to be in a method..

    class Class1
        {
            public void DoQstuff()
            {
              Queue numbers = new Queue();
    
              foreach (int number in new int[4]{1,2,3,4})
              {
                  numbers.Enqueue(number);
                  Console.WriteLine(number + " has joined the queue");
              }
              foreach (int number in numbers)
              {
                  Console.WriteLine(number);
              }
              while(numbers.Count > 0)
              {
                  int number = (int)numbers.Dequeue();
                  Console.WriteLine(number + " has left the queue");
              }
            }
        }
    

    and the whole thing works as expected if you run it

    Queues work, first in, first out….. so 1 was first in, so its first out

    Stacks work, first in, last out, so 1 was the first thing in, so it will be the last thing out

    like, if you line up to be served at a coffee shop, you have to wait in queue till your turn, first person in the queue, is the first person served.

    stacks is like stacking books on top of each other….you can’t take the first thing off the stack until you unstack all the things on top of it.

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

Sidebar

Related Questions

I have a method that gets called into a new thread like so: if
Greetings, creating my first MVVM based WPF app and trying to figure out why
what is the best way to identify first row in the following code? foreach(DataRow
Since C# doesn't have a before,after,last,first etc. as part of its foreach. The challenge
First, let's get the security considerations out of the way. I'm using simple authentication
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
I have a method that list listing directories and you can drill in the
Problem 2b goes as follows: 2b. For each subject show the first year that
First off, I am using Windows XP. I have multiple hard drives and it
First off, I understand the reasons why an interface or abstract class (in the

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.