C# - The Queue Class

The Queue class provides a number of properties and methods that are used to work with a queue, which is an abstract data structure that uses a first in, first out (FIFO) structure. A queue operates much like a line at a supermarket. The Queue class properties and methods are listed below.

Properties and Methods of the Queue Class

Item Description
Properties
Count Contains the number of items currently in the queue
IsReadOnly Contains True if the queue is read-only and False otherwise
IsSynchronized Contains True if the queue is synchronized and False otherwise
Methods
Clear() Clear all items from the queue
Clone() Creates a duplicate of the queue
Contains() Returns True if a given object is in the current queue
CopyTo() Copies all or part of the current queue to another queue that is passed in as an argument
Dequeue() Returns the next item from the queue and then removes it from the queue
Enqueue() Adds a new object to the end of the queue
Peek() Returns the next item from the queue but does not remove it
Synchronized() Synchronizes the queue
ToArray() Copies all or part of the current queue to an array
Queue.Count

Syntax

Int32 Count

Description

The Queue.Count property contains the number of objects currently in the queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

MyLabel.Text = "Count is "+q.Count.ToString();
Queue.IsReadOnly

Syntax

Boolean IsReadOnly

Description

The Queue.IsReadOnly property enables a developer to check the read-only status of a queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

MyLabel.Text = "IsReadOnly set to " + q.IsReadOnly;
Queue.IsSynchronized

Syntax

Boolean IsSynchronized

Description

The Queue.IsSynchronized property enables a developer to check the synchronization of a queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

MyLabel.Text = "IsSynchronized set to " + q.IsSynchronized;
Queue.Clear()

Syntax

Void Clear()

Description

The Queue.Clear() method removes all items from the queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

q.Clear();
Queue.Clone()

Syntax

Object Clone()

Description

The Queue.Clone() method returns a queue that is identical to the one being cloned.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

Queue q2 = (Queue)q.Clone();
Queue.Contains()

Syntax

Boolean Contains( Object obj )

Description

The Queue.Contains() method returns True if the argument obj is found in the queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

MyLabel.Text = "Does queue contain this object: "+q.Contains("item2").ToString();
Queue.CopyTo()

Syntax

Void CopyTo(Array array, Int32 index)

Description

The Queue.CopyTo() method copies a queue to an array.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

String[] MyArray = new String[3];
q.CopyTo(MyArray, 0);
MyLabel.Text = "Third item of queue is "+MyArray[2];
Queue.Dequeue()

Syntax

Object Dequeue()

Description

The Queue.Dequeue() method returns the object at the front of the queue and then removes it from the queue. If the queue is empty, InvalidOperationException is thrown at runtime.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

q.Dequeue();
Queue.Enqueue()

Syntax

Void Enqueue( Object obj )

Description

The Queue.Enqueue() method puts an object or a value at the end of the queue.

Example

Queue q = new Queue();
q.Enqueue(35);
Queue.Peek()

Syntax

Object Peek()

Description

The Queue.Peek() method returns the object to the front of the queue, but it does not remove this object from the queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

MyLabel.Text = "The next object in Queue is '"+q.peek()+"'";
Queue.Synchronized()

Syntax

Queue Synchronized( Queue queue )

Description

The Queue.Synchronized() method returns a synchronized copy of the current queue.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

q = Queue.Synchronized(q);
MyLabel.Text = "True if items is synchronized: "+q.IsSynchronized;
Queue.ToArray()

Syntax

Object[] ToArray()

Description

The Queue.ToArray() method copies the current queue to an object array.

Example

Queue q = new Queue();
q.Enqueue("item1");
q.Enqueue("item2");
q.Enqueue("item3");

Object[] MyArray;
MyArray = q.ToArray();