How to get value at certain index in a queue(C#)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

20th Jan 2023, 3:40 PM
Ilhan
Ilhan - avatar
1 Answer
+ 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.
20th Jan 2023, 4:50 PM
JaScript
JaScript - avatar