C# Queue

Queue is a class handles a circular array. It add element to the end, and remove element from the start position. It's useful when you want to store some data temporarily. For example, discard the data when its value has been retrieved.

using System.Collections;
Queue q = new Queue();
q.Enqueue("John");
q.Enqueue("Richard");
q.Enqueue("Sophia");

Queue do not implement IList, ICollection, so it do not has Add, Remove and Sort methods. It use Dequeue to read one element from the start position, and at the same time delete it.
q.Dequeue();
q.Peek(); //read one from the start, but not delete it

Queue generics:
using System.Collections.Generic;
Queue<string> q= new Queue<string>();
q.Enqueue("John");
q.Enqueue("Richard");
q.Enqueue("Sophia");
string str = q.Dequeue(); //str="John"
q.Count; //2
q.Contains("Sohpia"); //true


endmemo.com © 2024  | Terms of Use | Privacy | Home