1. Knowledge points involved
kind
Queue class Queue is indeed provided in C#. A queue is a first-in-first-out (FIFO) data structure that is used to store and operate on an ordered set of objects. In C#, you can use the Queue<T> class in the namespace to implement the queue. Queues are very useful in storing messages in received order for sequential processing. The Queue class implements the queue as a loop array, and objects stored in the Queue class are inserted at one end and removed from the other end.
// Create a new Queue<int> instanceQueue<int> queue = new Queue<int>(); //or // Create a new Queue<int> instanceQueue<int> myQueue = new();
Methods and properties of <T> class
The Queue<T> class provides many methods and properties for handling elements in a queue. Here are some commonly used methods:
- Enqueue(T item): Add an element to the end of the queue.
- Dequeue(): Removes the element from the beginning of the queue and returns it.
- Peek(): View elements at the beginning of the queue, but do not remove them.
- Clear(): Removes all elements in the queue.
- Contains(T item): Check whether the queue contains a specific element.
- CopyTo(T[] array, int arrayIndex): Copy elements in the queue into an array.
- Count: Gets the number of elements in the queue.
2. Examples of using C# Queue class
This example demonstrates how to create a Queue<int> instance, add elements to it, view elements, remove elements from the queue, view elements again, add new elements to the queue, view elements at the beginning of the queue, clear the queue, and check if the queue is empty.
// Use C# Queue class instancenamespace _134_1 { class Program { static void Main(string[] args) { (args); // Create a new Queue<int> instance Queue<int> myQueue = new(); // Add some elements to the queue (1); (2); (3); // View elements in the queue ("Elements in the queue:"); foreach (int element in myQueue) { (element + " "); } // Remove from the queue and return the first element int dequeuedElement = (); ("\nRemoved elements:{0}", dequeuedElement); // Check the elements in the queue again ("\nElements in the queue (after updated):"); foreach (int element in myQueue) { (element + " "); } // Add a new element to the queue (4); // Check the elements in the queue again ("\nElements in the queue (after updated):"); foreach (int element in myQueue) { (element + " "); } // View the elements at the beginning of the queue ("\nElements at the beginning of the queue:{0}", ()); // Clear the queue (); // Check whether the queue is empty ("\nIs the queue empty?:{0}", == 0); } } } //Run result:/* Elements in the queue: 1 2 3 Removed elements: 1 Elements in the queue (after updated): twenty three Elements in the queue (after updated): 2 3 4 Elements at the beginning of the queue: 2 Whether the queue is empty: True */
This is the end of this article about C# using Queue<T> for queue design. For more related C# Queue<T> queue content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!