Home > Mobile >  How to display all customers in a queue in c#?
How to display all customers in a queue in c#?

Time:10-03

So I am writing a c# console which uses a queue to display people in a line. Here is the code:

 customers.Enqueue("Jim");
                    customers.Enqueue("Bob");
                    customers.Enqueue("Susan");
                    customers.Enqueue("Liz");
                    customers.Enqueue("Mary");

                    Console.WriteLine("The number of the people in line at the bank is: ");
                 
                    Console.WriteLine(customers.Count);
                    Console.WriteLine("The names of those in line at the bank are: "   customers.Peek());

                    customers.Enqueue("Andyn");
                    customers.Enqueue("Rhonda");


                    customers.Dequeue();
                    customers.Dequeue();
                    customers.Dequeue();
                    Console.WriteLine("The number of shoppers now in line is:  "   customers.Count);
                    Console.WriteLine("The shopper currently first in line is: "   customers.Peek());

                    Console.ReadKey();

It works well, the only issue is when trying to display the names of the people in line, it only displays Jim. I am wondering how to display the others in the line as well? Thank you.

CodePudding user response:

you can use foreach loop

foreach (var customer in customers)
    {
        Console.WriteLine(customer);
    }

result

Liz
Mary
Andyn
Rhonda

CodePudding user response:

Queue implements IEnumerable, which means you can pass it to string.Join to create a single string of all elements joined by a common delimiter:

var q = string.Join(",", customers);

You can then print this out

  • Related