[Serializable] |
To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Stack.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.
If Stack.Count is less than the capacity of the stack, Stack.Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Stack.Push becomes an O(n) operation, where n is Stack.Count. Stack.Pop is an O(1) operation.
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine( "myStack" ); Console.WriteLine( "\tCount: {0}", myStack.Count ); Console.Write( "\tValues:" ); PrintValues( myStack ); } 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. myStack Count: 3 Values: ! World Hello */
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 Stack class that is empty and has the default initial capacity. |
ctor #2 | Overloaded:.ctor(ICollection col) Initializes a new instance of the Stack class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied. |
ctor #3 | Overloaded:.ctor(int initialCapacity) Initializes a new instance of the Stack class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater. |
Count | Read-only Gets the number of elements contained in the Stack. |
IsSynchronized | Read-only Gets a value indicating whether access to the Stack is synchronized (thread-safe). |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the Stack. |
Clear | Removes all objects from the Stack. |
Clone | Creates a shallow copy of the Stack. |
Contains | Determines whether an element is in the Stack. |
CopyTo | Copies the Stack to an existing one-dimensional Array, starting at the specified array index. |
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 IEnumerator for the Stack. |
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 top of the Stack without removing it. |
Pop | Removes and returns the object at the top of the Stack. |
Push | Inserts an object at the top of the Stack. |
Synchronized | Returns a synchronized (thread-safe) wrapper for the Stack. |
ToArray | Copies the Stack 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. |
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 Stack(); |
If the number of elements added to the stack reaches the current capacity, the capacity is automatically doubled.
public Stack( |
col
Exception Type | Condition |
---|---|
ArgumentNullException | col is null. |
The elements are copied onto the Stack in the same order they are read by the IEnumerator of the ICollection.
public Stack( |
initialCapacity
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | initialCapacity is less than zero. |
If the number of elements added to the stack reaches the current capacity, the capacity is automatically doubled. Therefore, 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 Stack.
public virtual int Count {get;}
|
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 Stack.SyncRoot during the entire enumeration:
Stack myCollection = new Stack(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Creates a synchronized wrapper around the Stack. Stack mySyncdStack = Stack.Synchronized( myStack ); // Displays the sychronization status of both Stacks. Console.WriteLine( "myStack is {0}.", myStack.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdStack is {0}.", mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myStack is not synchronized. mySyncdStack 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 Stack.SyncRoot during the entire enumeration:
Stack myCollection = new Stack(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public virtual void Clear(); |
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); myStack.Push( "jumped" ); // Displays the count and values of the Stack. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", myStack.Count ); Console.Write( " Values:" ); PrintValues( myStack ); // Clears the Stack. myStack.Clear(); // Displays the count and values of the Stack. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", myStack.Count ); Console.Write( " Values:" ); PrintValues( myStack ); } 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: jumped fox brown quick The 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 Stack is greater than the available space from index to the end of the destination array. |
InvalidCastException | The type of the source Stack cannot be cast automatically to the type of the destination array. |
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes the source Stack. Stack mySourceQ = new Stack(); mySourceQ.Push( "barn" ); mySourceQ.Push( "the" ); mySourceQ.Push( "in" ); mySourceQ.Push( "cats" ); mySourceQ.Push( "napping" ); mySourceQ.Push( "three" ); // 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 Stack 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 Stack 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 */
~Stack(); |
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 Stack is empty. |
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes an element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes another element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Views the first element in the Stack but does not remove it. Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\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. Stack values: fox brown quick The (Pop) fox Stack values: brown quick The (Pop) brown Stack values: quick The (Peek) quick Stack values: quick The */
public virtual object Pop(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The Stack is empty. |
null can be pushed onto the Stack as a placeholder, if needed. To distinguish between a null value and the end of the stack, check the Stack.Count property or catch the InvalidOperationException, which is thrown when the Stack is empty.
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes an element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes another element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Views the first element in the Stack but does not remove it. Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\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. Stack values: fox brown quick The (Pop) fox Stack values: brown quick The (Pop) brown Stack values: quick The (Peek) quick Stack values: quick The */
public virtual void Push( |
obj
If Stack.Count is less than the capacity of the stack, Stack.Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Stack.Push becomes an O(n) operation, where n is Stack.Count.
null can be pushed onto the Stack as a placeholder, if needed. It occupies a slot in the stack and is treated like any object.
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes an element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Removes another element from the Stack. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\t' ); // Views the first element in the Stack but does not remove it. Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() ); // Displays the Stack. Console.Write( "Stack values:" ); PrintValues( myStack, '\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. Stack values: fox brown quick The (Pop) fox Stack values: brown quick The (Pop) brown Stack values: quick The (Peek) quick Stack values: quick The */
stack
Exception Type | Condition |
---|---|
ArgumentNullException | stack 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 Stack.SyncRoot during the entire enumeration:
Stack myCollection = new Stack(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Creates a synchronized wrapper around the Stack. Stack mySyncdStack = Stack.Synchronized( myStack ); // Displays the sychronization status of both Stacks. Console.WriteLine( "myStack is {0}.", myStack.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdStack is {0}.", mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myStack is not synchronized. mySyncdStack is synchronized. */
public virtual object[] ToArray(); |
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes the source Stack. Stack mySourceQ = new Stack(); mySourceQ.Push( "barn" ); mySourceQ.Push( "the" ); mySourceQ.Push( "in" ); mySourceQ.Push( "cats" ); mySourceQ.Push( "napping" ); mySourceQ.Push( "three" ); // 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 Stack 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 Stack 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(); |