[Serializable] |
To guarantee the thread safety of the Queue, all operations must be done through the wrapper returned by the Queue.Synchronized method.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue("Hello"); myQ.Enqueue("World"); myQ.Enqueue("!"); // Displays the properties and values of the Queue. Console.WriteLine( "myQ" ); Console.WriteLine( "\tCount: {0}", myQ.Count ); Console.Write( "\tValues:" ); PrintValues( myQ ); } public static void PrintValues( IEnumerable myCollection ) { System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\t{0}", myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. myQ Count: 3 Values: Hello World ! */
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the Queue class that is empty, has the default initial capacity and uses the default growth factor. |
ctor #2 | Overloaded:.ctor(ICollection col) Initializes a new instance of the Queue class that contains elements copied from the specified collection, has the same initial capacity as the number of elements copied and uses the default growth factor. |
ctor #3 | Overloaded:.ctor(int capacity) Initializes a new instance of the Queue class that is empty, has the specified initial capacity and uses the default growth factor. |
ctor #4 | Overloaded:.ctor(int capacity, float growFactor) Initializes a new instance of the Queue class that is empty, has the specified initial capacity and uses the specified growth factor. |
Count | Read-only Gets the number of elements contained in the Queue. |
IsSynchronized | Read-only Gets a value indicating whether access to the Queue is synchronized (thread-safe). |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the Queue. |
Clear | Removes all objects from the Queue. |
Clone | Creates a shallow copy of the Queue. |
Contains | Determines whether an element is in the Queue. |
CopyTo | Copies the Queue elements to an existing one-dimensional Array, starting at the specified array index. |
Dequeue | Removes and returns the object at the beginning of the Queue. |
Enqueue | Adds an object to the end of the Queue. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetEnumerator | Returns an enumerator that can iterate through the Queue. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Peek | Returns the object at the beginning of the Queue without removing it. |
Synchronized | Returns a Queue wrapper that is synchronized (thread-safe). |
ToArray | Copies the Queue elements to a new array. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
TrimToSize | Sets the capacity to the actual number of elements in the Queue. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public Queue(); |
If the number of elements added to the Queue reaches the current capacity, the capacity is automatically increased. The new capacity is determined by multiplying the current capacity by the growth factor.
public Queue( |
col
Exception Type | Condition |
---|---|
ArgumentNullException | col is null. |
The elements are copied onto the Queue in the same order they are read by the IEnumerator of the ICollection.
If the number of elements added to the Queue reaches the current capacity, the capacity is automatically increased. The new capacity is determined by multiplying the current capacity by the growth factor.
public Queue( |
capacity
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is less than zero. |
If the number of elements added to the Queue reaches the current capacity, the capacity is automatically increased. The new capacity is determined by multiplying the current capacity by the growth factor.
If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Queue.
capacity
growFactor
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is less than zero. -or- growFactor is less than 1.0 or greater than 10.0. |
If the number of elements added to the Queue reaches the current capacity, the capacity is automatically increased. The new capacity is determined by multiplying the current capacity by the growth factor.
If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Queue.
public virtual int Count {get;}
|
Queue.Count is always less than or equal to the capacity of the Queue. If Queue.Count exceeds the capacity of the Queue while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new element. The new capacity is determined by multiplying the current capacity by the growth factor, which is determined when the Queue is constructed.
public virtual bool IsSynchronized {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the Queue.SyncRoot during the entire enumeration:
Queue myCollection = new Queue(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); // Creates a synchronized wrapper around the Queue. Queue mySyncdQ = Queue.Synchronized( myQ ); // Displays the sychronization status of both Queues. Console.WriteLine( "myQ is {0}.", myQ.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdQ is {0}.", mySyncdQ.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myQ is not synchronized. mySyncdQ is synchronized. */
public virtual object SyncRoot {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the Queue.SyncRoot during the entire enumeration:
Queue myCollection = new Queue(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public virtual void Clear(); |
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); myQ.Enqueue( "jumped" ); // Displays the count and values of the Queue. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", myQ.Count ); Console.Write( " Values:" ); PrintValues( myQ ); // Clears the Queue. myQ.Clear(); // Displays the count and values of the Queue. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", myQ.Count ); Console.Write( " Values:" ); PrintValues( myQ ); } public static void PrintValues( IEnumerable myCollection ) { System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\t{0}", myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. Initially, Count : 5 Values: The quick brown fox jumped After Clear, Count : 0 Values: */
public virtual object Clone(); |
In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.
obj
This method determines equality by calling Object.Equals.
array
index
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | index is less than zero. |
ArgumentException | array is multidimensional. -or- index is equal to or greater than the length of array. -or- The number of elements in the source Queue is greater than the available space from index to the end of the destination array. |
ArrayTypeMismatchException | The type of the source Queue cannot be cast automatically to the type of the destination array. |
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes the source Queue. Queue mySourceQ = new Queue(); mySourceQ.Enqueue( "three" ); mySourceQ.Enqueue( "napping" ); mySourceQ.Enqueue( "cats" ); mySourceQ.Enqueue( "in" ); mySourceQ.Enqueue( "the" ); mySourceQ.Enqueue( "barn" ); // Creates and initializes the one-dimensional target Array. Array myTargetArray=Array.CreateInstance( typeof(String), 15 ); myTargetArray.SetValue( "The", 0 ); myTargetArray.SetValue( "quick", 1 ); myTargetArray.SetValue( "brown", 2 ); myTargetArray.SetValue( "fox", 3 ); myTargetArray.SetValue( "jumped", 4 ); myTargetArray.SetValue( "over", 5 ); myTargetArray.SetValue( "the", 6 ); myTargetArray.SetValue( "lazy", 7 ); myTargetArray.SetValue( "dog", 8 ); // Displays the values of the target Array. Console.WriteLine( "The target Array contains the following (before and after copying):" ); PrintValues( myTargetArray, ' ' ); // Copies the entire source Queue to the target Array, starting at index 6. mySourceQ.CopyTo( myTargetArray, 6 ); // Displays the values of the target Array. PrintValues( myTargetArray, ' ' ); // Copies the entire source Queue to a new standard array. Object[] myStandardArray = mySourceQ.ToArray(); // Displays the values of the new standard array. Console.WriteLine( "The new standard array contains the following:" ); PrintValues( myStandardArray, ' ' ); } public static void PrintValues( Array myArr, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator(); int i = 0; int cols = myArr.GetLength( myArr.Rank - 1 ); while ( myEnumerator.MoveNext() ) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); } Console.WriteLine(); } public static void PrintValues( Object[] myArr, char mySeparator ) { foreach ( Object myObj in myArr ) { Console.Write( "{0}{1}", mySeparator, myObj ); } Console.WriteLine(); } } /* This code produces the following output. The target Array contains the following (before and after copying): The quick brown fox jumped over the lazy dog The quick brown fox jumped over three napping cats in the barn The new standard array contains the following: three napping cats in the barn */
public virtual object Dequeue(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The Queue is empty. |
This method is an O(1) operation.
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Removes an element from the Queue. Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Removes another element from the Queue. Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Views the first element in the Queue but does not remove it. Console.WriteLine( "(Peek) \t{0}", myQ.Peek() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); } public static void PrintValues( IEnumerable myCollection, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. Queue values: The quick brown fox (Dequeue) The Queue values: quick brown fox (Dequeue) quick Queue values: brown fox (Peek) brown Queue values: brown fox */
public virtual void Enqueue( |
obj
If Queue.Count is less than the capacity of the internal array, this method is an O(1) operation. If the internal array needs to be reallocated to accommodate the new element, this method becomes an O(n) operation, where n is Queue.Count.
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Removes an element from the Queue. Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Removes another element from the Queue. Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Views the first element in the Queue but does not remove it. Console.WriteLine( "(Peek) \t{0}", myQ.Peek() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); } public static void PrintValues( IEnumerable myCollection, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. Queue values: The quick brown fox (Dequeue) The Queue values: quick brown fox (Dequeue) quick Queue values: brown fox (Peek) brown Queue values: brown fox */
~Queue(); |
public virtual IEnumerator GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual object Peek(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The Queue is empty. |
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Removes an element from the Queue. Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Removes another element from the Queue. Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); // Views the first element in the Queue but does not remove it. Console.WriteLine( "(Peek) \t{0}", myQ.Peek() ); // Displays the Queue. Console.Write( "Queue values:" ); PrintValues( myQ, '\t' ); } public static void PrintValues( IEnumerable myCollection, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. Queue values: The quick brown fox (Dequeue) The Queue values: quick brown fox (Dequeue) quick Queue values: brown fox (Peek) brown Queue values: brown fox */
queue
Exception Type | Condition |
---|---|
ArgumentNullException | queue is null. |
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the Queue.SyncRoot during the entire enumeration:
Queue myCollection = new Queue(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue( "The" ); myQ.Enqueue( "quick" ); myQ.Enqueue( "brown" ); myQ.Enqueue( "fox" ); // Creates a synchronized wrapper around the Queue. Queue mySyncdQ = Queue.Synchronized( myQ ); // Displays the sychronization status of both Queues. Console.WriteLine( "myQ is {0}.", myQ.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdQ is {0}.", mySyncdQ.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myQ is not synchronized. mySyncdQ is synchronized. */
public virtual object[] ToArray(); |
using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes the source Queue. Queue mySourceQ = new Queue(); mySourceQ.Enqueue( "three" ); mySourceQ.Enqueue( "napping" ); mySourceQ.Enqueue( "cats" ); mySourceQ.Enqueue( "in" ); mySourceQ.Enqueue( "the" ); mySourceQ.Enqueue( "barn" ); // Creates and initializes the one-dimensional target Array. Array myTargetArray=Array.CreateInstance( typeof(String), 15 ); myTargetArray.SetValue( "The", 0 ); myTargetArray.SetValue( "quick", 1 ); myTargetArray.SetValue( "brown", 2 ); myTargetArray.SetValue( "fox", 3 ); myTargetArray.SetValue( "jumped", 4 ); myTargetArray.SetValue( "over", 5 ); myTargetArray.SetValue( "the", 6 ); myTargetArray.SetValue( "lazy", 7 ); myTargetArray.SetValue( "dog", 8 ); // Displays the values of the target Array. Console.WriteLine( "The target Array contains the following (before and after copying):" ); PrintValues( myTargetArray, ' ' ); // Copies the entire source Queue to the target Array, starting at index 6. mySourceQ.CopyTo( myTargetArray, 6 ); // Displays the values of the target Array. PrintValues( myTargetArray, ' ' ); // Copies the entire source Queue to a new standard array. Object[] myStandardArray = mySourceQ.ToArray(); // Displays the values of the new standard array. Console.WriteLine( "The new standard array contains the following:" ); PrintValues( myStandardArray, ' ' ); } public static void PrintValues( Array myArr, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator(); int i = 0; int cols = myArr.GetLength( myArr.Rank - 1 ); while ( myEnumerator.MoveNext() ) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); } Console.WriteLine(); } public static void PrintValues( Object[] myArr, char mySeparator ) { foreach ( Object myObj in myArr ) { Console.Write( "{0}{1}", mySeparator, myObj ); } Console.WriteLine(); } } /* This code produces the following output. The target Array contains the following (before and after copying): The quick brown fox jumped over the lazy dog The quick brown fox jumped over three napping cats in the barn The new standard array contains the following: three napping cats in the barn */
public virtual string ToString(); |
public virtual void TrimToSize(); |
Exception Type | Condition |
---|---|
NotSupportedException | The Queue is read-only. |
To completely clear all elements in a list, call the Queue.Clear method before calling Queue.TrimToSize. Trimming an empty Queue sets the capacity of the Queue to the default capacity, not zero.