+ 1
How to get value at certain index in a queue(C#)?
So i declare a queue "q" and populate it with few members without dequeuing them.When i try to access a member by using q[1] it doesnt work but when i use it in a foreach loop it can get every member of the queue q.How does the foreach loop get access to each member individualy when technicaly you can only return the first member by using Peek() method?
1 Réponse
+ 3
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
// Please save your code on Sololearns Playground and link here.