Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
1354 lines (1143 sloc) 96.3 KB
<Type Name="Stack&lt;T&gt;" FullName="System.Collections.Generic.Stack&lt;T&gt;">
<TypeSignature Language="C#" Value="public class Stack&lt;T&gt; : System.Collections.Generic.IEnumerable&lt;T&gt;, System.Collections.Generic.IReadOnlyCollection&lt;T&gt;, System.Collections.ICollection" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi serializable beforefieldinit Stack`1&lt;T&gt; extends System.Object implements class System.Collections.Generic.IEnumerable`1&lt;!T&gt;, class System.Collections.Generic.IReadOnlyCollection`1&lt;!T&gt;, class System.Collections.ICollection, class System.Collections.IEnumerable" />
<TypeSignature Language="DocId" Value="T:System.Collections.Generic.Stack`1" />
<TypeSignature Language="VB.NET" Value="Public Class Stack(Of T)&#xA;Implements ICollection, IEnumerable(Of T), IReadOnlyCollection(Of T)" />
<TypeSignature Language="C++ CLI" Value="generic &lt;typename T&gt;&#xA;public ref class Stack : System::Collections::Generic::IEnumerable&lt;T&gt;, System::Collections::Generic::IReadOnlyCollection&lt;T&gt;, System::Collections::ICollection" />
<TypeSignature Language="F#" Value="type Stack&lt;'T&gt; = class&#xA; interface seq&lt;'T&gt;&#xA; interface ICollection&#xA; interface IReadOnlyCollection&lt;'T&gt;&#xA; interface IEnumerable" />
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<TypeParameters>
<TypeParameter Name="T" />
</TypeParameters>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces>
<Interface>
<InterfaceName>System.Collections.Generic.IEnumerable&lt;T&gt;</InterfaceName>
</Interface>
<Interface>
<InterfaceName>System.Collections.Generic.IReadOnlyCollection&lt;T&gt;</InterfaceName>
</Interface>
<Interface>
<InterfaceName>System.Collections.ICollection</InterfaceName>
</Interface>
<Interface>
<InterfaceName>System.Collections.IEnumerable</InterfaceName>
</Interface>
</Interfaces>
<Attributes>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;xamarinandroid-7.1;xamarinios-10.8;xamarinmac-3.0;netframework-4.8">
<AttributeName>System.Diagnostics.DebuggerDisplay("Count = {Count}")</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;xamarinandroid-7.1;xamarinios-10.8;xamarinmac-3.0;netframework-4.8">
<AttributeName>System.Diagnostics.DebuggerTypeProxy(typeof(System.Collections.Generic.System_StackDebugView`1))</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;xamarinandroid-7.1;xamarinios-10.8;xamarinmac-3.0;netframework-4.8">
<AttributeName>System.Runtime.InteropServices.ComVisible(false)</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;xamarinandroid-7.1;xamarinios-10.8;xamarinmac-3.0">
<AttributeName>System.Serializable</AttributeName>
</Attribute>
</Attributes>
<Docs>
<typeparam name="T">Specifies the type of elements in the stack.</typeparam>
<summary>Represents a variable size last-in-first-out (LIFO) collection of instances of the same specified type.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Collections.Generic.Stack%601> is implemented as an array.
Stacks and queues are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use <xref:System.Collections.Generic.Queue%601> if you need to access the information in the same order that it is stored in the collection. Use <xref:System.Collections.Generic.Stack%601?displayProperty=nameWithType> if you need to access the information in reverse order.
Use the <xref:System.Collections.Concurrent.ConcurrentStack%601?displayProperty=nameWithType> and <xref:System.Collections.Concurrent.ConcurrentQueue%601?displayProperty=nameWithType> types when you need to access the collection from multiple threads concurrently.
A common use for <xref:System.Collections.Generic.Stack%601?displayProperty=nameWithType> is to preserve variable states during calls to other procedures.
Three main operations can be performed on a <xref:System.Collections.Generic.Stack%601?displayProperty=nameWithType> and its elements:
- <xref:System.Collections.Generic.Stack%601.Push%2A> inserts an element at the top of the <xref:System.Collections.Stack>.
- <xref:System.Collections.Generic.Stack%601.Pop%2A> removes an element from the top of the <xref:System.Collections.Generic.Stack%601>.
- <xref:System.Collections.Generic.Stack%601.Peek%2A> returns an element that is at the top of the <xref:System.Collections.Generic.Stack%601> but does not remove it from the <xref:System.Collections.Generic.Stack%601>.
The capacity of a <xref:System.Collections.Generic.Stack%601> is the number of elements the <xref:System.Collections.Generic.Stack%601> can hold. As elements are added to a <xref:System.Collections.Generic.Stack%601>, the capacity is automatically increased as required by reallocating the internal array. The capacity can be decreased by calling <xref:System.Collections.Generic.Stack%601.TrimExcess%2A>.
If <xref:System.Collections.Generic.Stack%601.Count%2A> is less than the capacity of the stack, <xref:System.Collections.Generic.Stack%601.Push%2A> is an O(1) operation. If the capacity needs to be increased to accommodate the new element, <xref:System.Collections.Generic.Stack%601.Push%2A> becomes an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>. <xref:System.Collections.Generic.Stack%601.Pop%2A> is an O(1) operation.
<xref:System.Collections.Generic.Stack%601> accepts `null` as a valid value for reference types and allows duplicate elements.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class. The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<threadsafe>Public static (<see langword="Shared" /> in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
A <see cref="T:System.Collections.Generic.Stack`1" /> can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.</threadsafe>
<related type="Article" href="https://msdn.microsoft.com/library/f45331db-d595-46ec-9142-551d3d1eb1a7">Iterators (C# and Visual Basic)</related>
</Docs>
<Members>
<MemberGroup MemberName=".ctor">
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class.</summary>
</Docs>
</MemberGroup>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Stack ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.#ctor" />
<MemberSignature Language="VB.NET" Value="Public Sub New ()" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; Stack();" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class that is empty and has the default initial capacity.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The capacity of a <xref:System.Collections.Generic.Stack%601> is the number of elements that the <xref:System.Collections.Generic.Stack%601> can hold. As elements are added to a <xref:System.Collections.Generic.Stack%601>, the capacity is automatically increased as required by reallocating the internal array.
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 <xref:System.Collections.Generic.Stack%601>.
The capacity can be decreased by calling <xref:System.Collections.Generic.Stack%601.TrimExcess%2A>.
This constructor is an O(1) operation.
## Examples
The following code example demonstrates this constructor and several methods of the <xref:System.Collections.Generic.Stack%601> generic class.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Stack (System.Collections.Generic.IEnumerable&lt;T&gt; collection);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Collections.Generic.IEnumerable`1&lt;!T&gt; collection) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.#ctor(System.Collections.Generic.IEnumerable{`0})" />
<MemberSignature Language="VB.NET" Value="Public Sub New (collection As IEnumerable(Of T))" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; Stack(System::Collections::Generic::IEnumerable&lt;T&gt; ^ collection);" />
<MemberSignature Language="F#" Value="new System.Collections.Generic.Stack&lt;'T&gt; : seq&lt;'T&gt; -&gt; System.Collections.Generic.Stack&lt;'T&gt;" Usage="new System.Collections.Generic.Stack&lt;'T&gt; collection" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="collection" Type="System.Collections.Generic.IEnumerable&lt;T&gt;" />
</Parameters>
<Docs>
<param name="collection">The collection to copy elements from.</param>
<summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The capacity of a <xref:System.Collections.Generic.Stack%601> is the number of elements that the <xref:System.Collections.Generic.Stack%601> can hold. As elements are added to a <xref:System.Collections.Generic.Stack%601>, the capacity is automatically increased as required by reallocating the internal array.
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 <xref:System.Collections.Generic.Stack%601>.
The capacity can be decreased by calling <xref:System.Collections.Generic.Stack%601.TrimExcess%2A>.
The elements are copied onto the <xref:System.Collections.Generic.Stack%601> in the same order they are read by the <xref:System.Collections.Generic.IEnumerator%601> of the collection.
This constructor is an O(`n`) operation, where `n` is the number of elements in `collection`.
## Examples
The following code example demonstrates this constructor and several methods of the <xref:System.Collections.Generic.Stack%601> generic class.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="collection" /> is <see langword="null" />.</exception>
<altmember cref="T:System.Collections.Generic.ICollection`1" />
<altmember cref="T:System.Collections.Generic.IEnumerator`1" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Stack (int capacity);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(int32 capacity) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.#ctor(System.Int32)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (capacity As Integer)" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; Stack(int capacity);" />
<MemberSignature Language="F#" Value="new System.Collections.Generic.Stack&lt;'T&gt; : int -&gt; System.Collections.Generic.Stack&lt;'T&gt;" Usage="new System.Collections.Generic.Stack&lt;'T&gt; capacity" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="capacity" Type="System.Int32" />
</Parameters>
<Docs>
<param name="capacity">The initial number of elements that the <see cref="T:System.Collections.Generic.Stack`1" /> can contain.</param>
<summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The capacity of a <xref:System.Collections.Generic.Stack%601> is the number of elements that the <xref:System.Collections.Generic.Stack%601> can hold. As elements are added to a <xref:System.Collections.Generic.Stack%601>, the capacity is automatically increased as required by reallocating the internal array.
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 <xref:System.Collections.Generic.Stack%601>.
The capacity can be decreased by calling <xref:System.Collections.Generic.Stack%601.TrimExcess%2A>.
This constructor is an O(`n`) operation, where `n` is `capacity`.
]]></format>
</remarks>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="capacity" /> is less than zero.</exception>
</Docs>
</Member>
<Member MemberName="Clear">
<MemberSignature Language="C#" Value="public void Clear ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance void Clear() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.Clear" />
<MemberSignature Language="VB.NET" Value="Public Sub Clear ()" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; void Clear();" />
<MemberSignature Language="F#" Value="member this.Clear : unit -&gt; unit" Usage="stack.Clear " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Removes all objects from the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Collections.Generic.Stack%601.Count%2A> is set to zero, and references to other objects from elements of the collection are also released.
The capacity remains unchanged. To reset the capacity of the <xref:System.Collections.Generic.Stack%601>, call <xref:System.Collections.Generic.Stack%601.TrimExcess%2A>. Trimming an empty <xref:System.Collections.Generic.Stack%601> sets the capacity of the <xref:System.Collections.Generic.Stack%601> to the default capacity.
This method is an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.Clear%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="Contains">
<MemberSignature Language="C#" Value="public bool Contains (T item);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance bool Contains(!T item) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.Contains(`0)" />
<MemberSignature Language="VB.NET" Value="Public Function Contains (item As T) As Boolean" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; bool Contains(T item);" />
<MemberSignature Language="F#" Value="member this.Contains : 'T -&gt; bool" Usage="stack.Contains item" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="item" Type="T" />
</Parameters>
<Docs>
<param name="item">The object to locate in the <see cref="T:System.Collections.Generic.Stack`1" />. The value can be <see langword="null" /> for reference types.</param>
<summary>Determines whether an element is in the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
<returns>
<see langword="true" /> if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.Stack`1" />; otherwise, <see langword="false" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This method determines equality using the default equality comparer <xref:System.Collections.Generic.EqualityComparer%601.Default%2A?displayProperty=nameWithType> for `T`, the type of values in the list.
This method performs a linear search; therefore, this method is an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.Contains%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<related type="Article" href="~/docs/standard/globalization-localization/performing-culture-insensitive-string-operations-in-collections.md">Performing Culture-Insensitive String Operations in Collections</related>
</Docs>
</Member>
<Member MemberName="CopyTo">
<MemberSignature Language="C#" Value="public void CopyTo (T[] array, int arrayIndex);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance void CopyTo(!T[] array, int32 arrayIndex) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.CopyTo(`0[],System.Int32)" />
<MemberSignature Language="VB.NET" Value="Public Sub CopyTo (array As T(), arrayIndex As Integer)" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; void CopyTo(cli::array &lt;T&gt; ^ array, int arrayIndex);" />
<MemberSignature Language="F#" Value="member this.CopyTo : 'T[] * int -&gt; unit" Usage="stack.CopyTo (array, arrayIndex)" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="array" Type="T[]" />
<Parameter Name="arrayIndex" Type="System.Int32" />
</Parameters>
<Docs>
<param name="array">The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.Stack`1" />. The <see cref="T:System.Array" /> must have zero-based indexing.</param>
<param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
<summary>Copies the <see cref="T:System.Collections.Generic.Stack`1" /> to an existing one-dimensional <see cref="T:System.Array" />, starting at the specified array index.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to <xref:System.Collections.Generic.Stack%601.Pop%2A>.
This method is an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="array" /> is <see langword="null" />.</exception>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="arrayIndex" /> is less than zero.</exception>
<exception cref="T:System.ArgumentException">The number of elements in the source <see cref="T:System.Collections.Generic.Stack`1" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />.</exception>
<altmember cref="M:System.Collections.Generic.Stack`1.ToArray" />
</Docs>
</Member>
<Member MemberName="Count">
<MemberSignature Language="C#" Value="public int Count { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance int32 Count" />
<MemberSignature Language="DocId" Value="P:System.Collections.Generic.Stack`1.Count" />
<MemberSignature Language="VB.NET" Value="Public ReadOnly Property Count As Integer" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; property int Count { int get(); };" />
<MemberSignature Language="F#" Value="member this.Count : int" Usage="System.Collections.Generic.Stack&lt;'T&gt;.Count" />
<MemberType>Property</MemberType>
<Implements>
<InterfaceMember>P:System.Collections.ICollection.Count</InterfaceMember>
<InterfaceMember>P:System.Collections.Generic.IReadOnlyCollection`1.Count</InterfaceMember>
</Implements>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName>get: System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets the number of elements contained in the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
<value>The number of elements contained in the <see cref="T:System.Collections.Generic.Stack`1" />.</value>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The capacity of the <xref:System.Collections.Generic.Stack%601> is the number of elements that the <xref:System.Collections.Generic.Stack%601> can store. <xref:System.Collections.Generic.Stack%601.Count%2A> is the number of elements that are actually in the <xref:System.Collections.Generic.Stack%601>.
The capacity is always greater than or equal to <xref:System.Collections.Generic.Stack%601.Count%2A>. If <xref:System.Collections.Generic.Stack%601.Count%2A> exceeds the capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.
Retrieving the value of this property is an O(1) operation.
## Examples
The following code example demonstrates several properties and methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.Count%2A> property.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="GetEnumerator">
<MemberSignature Language="C#" Value="public System.Collections.Generic.Stack&lt;T&gt;.Enumerator GetEnumerator ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance valuetype System.Collections.Generic.Stack`1/Enumerator&lt;!T&gt; GetEnumerator() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.GetEnumerator" />
<MemberSignature Language="VB.NET" Value="Public Function GetEnumerator () As Stack(Of T).Enumerator" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; System::Collections::Generic::Stack&lt;T&gt;::Enumerator GetEnumerator();" />
<MemberSignature Language="F#" Value="member this.GetEnumerator : unit -&gt; System.Collections.Generic.Stack&lt;'T&gt;.Enumerator" Usage="stack.GetEnumerator " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Collections.Generic.Stack&lt;T&gt;+Enumerator</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Returns an enumerator for the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
<returns>An <see cref="T:System.Collections.Generic.Stack`1.Enumerator" /> for the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The `foreach` statement of the C# language (`for each` in C++, `For Each` in Visual Basic) hides the complexity of the enumerators. Therefore, using `foreach` is recommended, instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. At this position, <xref:System.Collections.Generic.Stack%601.Enumerator.Current%2A> is undefined. Therefore, you must call <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> to advance the enumerator to the first element of the collection before reading the value of <xref:System.Collections.Generic.Stack%601.Enumerator.Current%2A>.
<xref:System.Collections.Generic.Stack%601.Enumerator.Current%2A> returns the same object until <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> is called. <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> sets <xref:System.Collections.Generic.Stack%601.Enumerator.Current%2A> to the next element.
If <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> passes the end of the collection, the enumerator is positioned after the last element in the collection and <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> returns `false`. When the enumerator is at this position, subsequent calls to <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> also return `false`. If the last call to <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> returned `false`, <xref:System.Collections.Generic.Stack%601.Enumerator.Current%2A> is undefined. You cannot set <xref:System.Collections.Generic.Stack%601.Enumerator.Current%2A> to the first element of the collection again; you must create a new enumerator instance instead.
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 <xref:System.Collections.Generic.Stack%601.Enumerator.MoveNext%2A> or <xref:System.Collections.Generic.Stack%601.Enumerator.System%23Collections%23IEnumerator%23Reset%2A> throws an <xref:System.InvalidOperationException>.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Default implementations of collections in <xref:System.Collections.Generic?displayProperty=nameWithType> are not synchronized.
This method is an O(1) operation.
## Examples
The following code example demonstrates that the <xref:System.Collections.Generic.Stack%601> generic class is enumerable. The `foreach` statement (`For Each` in Visual Basic, `for each` in C++) is used to enumerate the queue.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<altmember cref="T:System.Collections.Generic.Stack`1.Enumerator" />
<altmember cref="T:System.Collections.Generic.IEnumerator`1" />
</Docs>
</Member>
<Member MemberName="Peek">
<MemberSignature Language="C#" Value="public T Peek ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance !T Peek() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.Peek" />
<MemberSignature Language="VB.NET" Value="Public Function Peek () As T" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; T Peek();" />
<MemberSignature Language="F#" Value="member this.Peek : unit -&gt; 'T" Usage="stack.Peek " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>T</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Returns the object at the top of the <see cref="T:System.Collections.Generic.Stack`1" /> without removing it.</summary>
<returns>The object at the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This method is similar to the <xref:System.Collections.Generic.Stack%601.Pop%2A> method, but <xref:System.Collections.Generic.Stack%601.Peek%2A> does not modify the <xref:System.Collections.Generic.Stack%601>.
If type `T` is a reference type, `null` can be pushed onto the <xref:System.Collections.Generic.Stack%601> as a placeholder, if needed.
This method is an O(1) operation.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.Peek%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<exception cref="T:System.InvalidOperationException">The <see cref="T:System.Collections.Generic.Stack`1" /> is empty.</exception>
<altmember cref="M:System.Collections.Generic.Stack`1.Pop" />
<altmember cref="M:System.Collections.Generic.Stack`1.Push(`0)" />
</Docs>
</Member>
<Member MemberName="Pop">
<MemberSignature Language="C#" Value="public T Pop ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance !T Pop() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.Pop" />
<MemberSignature Language="VB.NET" Value="Public Function Pop () As T" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; T Pop();" />
<MemberSignature Language="F#" Value="member this.Pop : unit -&gt; 'T" Usage="stack.Pop " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>T</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Removes and returns the object at the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
<returns>The object removed from the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This method is similar to the <xref:System.Collections.Generic.Stack%601.Peek%2A> method, but <xref:System.Collections.Generic.Stack%601.Peek%2A> does not modify the <xref:System.Collections.Generic.Stack%601>.
If type `T` is a reference type, `null` can be pushed onto the <xref:System.Collections.Generic.Stack%601> as a placeholder, if needed.
<xref:System.Collections.Generic.Stack%601> is implemented as an array. This method is an O(1) operation.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.Pop%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<exception cref="T:System.InvalidOperationException">The <see cref="T:System.Collections.Generic.Stack`1" /> is empty.</exception>
<altmember cref="M:System.Collections.Generic.Stack`1.Peek" />
<altmember cref="M:System.Collections.Generic.Stack`1.Push(`0)" />
</Docs>
</Member>
<Member MemberName="Push">
<MemberSignature Language="C#" Value="public void Push (T item);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance void Push(!T item) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.Push(`0)" />
<MemberSignature Language="VB.NET" Value="Public Sub Push (item As T)" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; void Push(T item);" />
<MemberSignature Language="F#" Value="member this.Push : 'T -&gt; unit" Usage="stack.Push item" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="item" Type="T" />
</Parameters>
<Docs>
<param name="item">The object to push onto the <see cref="T:System.Collections.Generic.Stack`1" />. The value can be <see langword="null" /> for reference types.</param>
<summary>Inserts an object at the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Collections.Generic.Stack%601> is implemented as an array.
If <xref:System.Collections.Generic.Stack%601.Count%2A> already equals the capacity, the capacity of the <xref:System.Collections.Generic.Stack%601> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.
If type `T` is a reference type, `null` can be pushed onto the <xref:System.Collections.Generic.Stack%601> as a placeholder, if needed. It occupies a slot in the stack and is treated like any object.
If <xref:System.Collections.Generic.Stack%601.Count%2A> is less than the capacity of the stack, <xref:System.Collections.Generic.Stack%601.Push%2A> is an O(1) operation. If the capacity needs to be increased to accommodate the new element, <xref:System.Collections.Generic.Stack%601.Push%2A> becomes an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.Push%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<altmember cref="M:System.Collections.Generic.Stack`1.Peek" />
<altmember cref="M:System.Collections.Generic.Stack`1.Pop" />
</Docs>
</Member>
<Member MemberName="System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator">
<MemberSignature Language="C#" Value="System.Collections.Generic.IEnumerator&lt;T&gt; IEnumerable&lt;T&gt;.GetEnumerator ();" />
<MemberSignature Language="ILAsm" Value=".method hidebysig newslot virtual instance class System.Collections.Generic.IEnumerator`1&lt;!T&gt; System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.System#Collections#Generic#IEnumerable&lt;T&gt;#GetEnumerator" />
<MemberSignature Language="VB.NET" Value="Function GetEnumerator () As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator" />
<MemberSignature Language="C++ CLI" Value=" virtual System::Collections::Generic::IEnumerator&lt;T&gt; ^ System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator() = System::Collections::Generic::IEnumerable&lt;T&gt;::GetEnumerator;" />
<MemberType>Method</MemberType>
<Implements>
<InterfaceMember>M:System.Collections.Generic.IEnumerable`1.GetEnumerator</InterfaceMember>
</Implements>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Collections.Generic.IEnumerator&lt;T&gt;</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Returns an enumerator that iterates through the collection.</summary>
<returns>An <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The `foreach` statement of the C# language (`for each` in C++, `For Each` in Visual Basic) hides the complexity of the enumerators. Therefore, using `foreach` is recommended, instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. At this position, <xref:System.Collections.Generic.IEnumerator%601.Current%2A> is undefined. Therefore, you must call <xref:System.Collections.IEnumerator.MoveNext%2A> to advance the enumerator to the first element of the collection before reading the value of <xref:System.Collections.Generic.IEnumerator%601.Current%2A>.
<xref:System.Collections.Generic.IEnumerator%601.Current%2A> returns the same object until <xref:System.Collections.IEnumerator.MoveNext%2A> is called. <xref:System.Collections.IEnumerator.MoveNext%2A> sets <xref:System.Collections.Generic.IEnumerator%601.Current%2A> to the next element.
If <xref:System.Collections.IEnumerator.MoveNext%2A> passes the end of the collection, the enumerator is positioned after the last element in the collection and <xref:System.Collections.IEnumerator.MoveNext%2A> returns `false`. When the enumerator is at this position, subsequent calls to <xref:System.Collections.IEnumerator.MoveNext%2A> also return `false`. If the last call to <xref:System.Collections.IEnumerator.MoveNext%2A> returned `false`, <xref:System.Collections.Generic.IEnumerator%601.Current%2A> is undefined. You cannot set <xref:System.Collections.Generic.IEnumerator%601.Current%2A> to the first element of the collection again; you must create a new enumerator instance instead.
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 <xref:System.Collections.IEnumerator.MoveNext%2A> or <xref:System.Collections.IEnumerator.Reset%2A> throws an <xref:System.InvalidOperationException>.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Default implementations of collections in <xref:System.Collections.Generic?displayProperty=nameWithType> are not synchronized.
This method is an O(1) operation.
]]></format>
</remarks>
<altmember cref="T:System.Collections.Generic.IEnumerator`1" />
</Docs>
</Member>
<Member MemberName="System.Collections.ICollection.CopyTo">
<MemberSignature Language="C#" Value="void ICollection.CopyTo (Array array, int arrayIndex);" />
<MemberSignature Language="ILAsm" Value=".method hidebysig newslot virtual instance void System.Collections.ICollection.CopyTo(class System.Array array, int32 arrayIndex) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.System#Collections#ICollection#CopyTo(System.Array,System.Int32)" />
<MemberSignature Language="C++ CLI" Value=" virtual void System.Collections.ICollection.CopyTo(Array ^ array, int arrayIndex) = System::Collections::ICollection::CopyTo;" />
<MemberType>Method</MemberType>
<Implements>
<InterfaceMember>M:System.Collections.ICollection.CopyTo(System.Array,System.Int32)</InterfaceMember>
</Implements>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="array" Type="System.Array" />
<Parameter Name="arrayIndex" Type="System.Int32" />
</Parameters>
<Docs>
<param name="array">The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection" />. The <see cref="T:System.Array" /> must have zero-based indexing.</param>
<param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
<summary>Copies the elements of the <see cref="T:System.Collections.ICollection" /> to an <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
> [!NOTE]
> If the type of the source <xref:System.Collections.ICollection> cannot be cast automatically to the type of the destination `array`, the non-generic implementations of <xref:System.Collections.ICollection.CopyTo%2A?displayProperty=nameWithType> throw <xref:System.InvalidCastException>, whereas the generic implementations throw <xref:System.ArgumentException>.
This method is an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="array" /> is <see langword="null" />.</exception>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="arrayIndex" /> is less than zero.</exception>
<exception cref="T:System.ArgumentException">
<paramref name="array" /> is multidimensional.
-or-
<paramref name="array" /> does not have zero-based indexing.
-or-
The number of elements in the source <see cref="T:System.Collections.ICollection" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />.
-or-
The type of the source <see cref="T:System.Collections.ICollection" /> cannot be cast automatically to the type of the destination <paramref name="array" />.</exception>
</Docs>
</Member>
<Member MemberName="System.Collections.ICollection.IsSynchronized">
<MemberSignature Language="C#" Value="bool System.Collections.ICollection.IsSynchronized { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance bool System.Collections.ICollection.IsSynchronized" />
<MemberSignature Language="DocId" Value="P:System.Collections.Generic.Stack`1.System#Collections#ICollection#IsSynchronized" />
<MemberSignature Language="VB.NET" Value=" ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized" />
<MemberSignature Language="C++ CLI" Value="property bool System::Collections::ICollection::IsSynchronized { bool get(); };" />
<MemberSignature Language="F#" Usage="System.Collections.ICollection.IsSynchronized" />
<MemberType>Property</MemberType>
<Implements>
<InterfaceMember>P:System.Collections.ICollection.IsSynchronized</InterfaceMember>
</Implements>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection" /> is synchronized (thread safe).</summary>
<value>
<see langword="true" /> if access to the <see cref="T:System.Collections.ICollection" /> is synchronized (thread safe); otherwise, <see langword="false" />. In the default implementation of <see cref="T:System.Collections.Generic.Stack`1" />, this property always returns <see langword="false" />.</value>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Default implementations of collections in <xref:System.Collections.Generic?displayProperty=nameWithType> are not synchronized.
Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where enumerations contend with write accesses, you must lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
<xref:System.Collections.ICollection.SyncRoot%2A> returns an object that can be used to synchronize access to the <xref:System.Collections.ICollection>. Synchronization is effective only if all threads lock this object before accessing the collection.
Retrieving the value of this property is an O(1) operation.
]]></format>
</remarks>
<altmember cref="P:System.Collections.ICollection.SyncRoot" />
</Docs>
</Member>
<Member MemberName="System.Collections.ICollection.SyncRoot">
<MemberSignature Language="C#" Value="object System.Collections.ICollection.SyncRoot { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance object System.Collections.ICollection.SyncRoot" />
<MemberSignature Language="DocId" Value="P:System.Collections.Generic.Stack`1.System#Collections#ICollection#SyncRoot" />
<MemberSignature Language="VB.NET" Value=" ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot" />
<MemberSignature Language="C++ CLI" Value="property System::Object ^ System::Collections::ICollection::SyncRoot { System::Object ^ get(); };" />
<MemberSignature Language="F#" Usage="System.Collections.ICollection.SyncRoot" />
<MemberType>Property</MemberType>
<Implements>
<InterfaceMember>P:System.Collections.ICollection.SyncRoot</InterfaceMember>
</Implements>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Object</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection" />.</summary>
<value>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection" />. In the default implementation of <see cref="T:System.Collections.Generic.Stack`1" />, this property always returns the current instance.</value>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Default implementations of collections in <xref:System.Collections.Generic?displayProperty=nameWithType> are not synchronized.
Enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
<xref:System.Collections.ICollection.SyncRoot%2A> returns an object that can be used to synchronize access to the <xref:System.Collections.ICollection>. Synchronization is effective only if all threads lock this object before accessing the collection. The following code shows the use of the <xref:System.Collections.ICollection.SyncRoot%2A> property for C#, C++, and Visual Basic.
```csharp
ICollection ic = ...;
lock (ic.SyncRoot) {
// Access the collection.
}
```
```vb
Dim ic As ICollection = ...
SyncLock ic.SyncRoot
' Access the collection.
End SyncLock
```
```cpp#
ICollection^ ic = ...;
try {
Monitor::Enter(ic->SyncRoot);
// Access the collection.
}
finally {
Monitor::Exit(ic->SyncRoot);
}
```
Retrieving the value of this property is an O(1) operation.
]]></format>
</remarks>
<altmember cref="P:System.Collections.ICollection.IsSynchronized" />
</Docs>
</Member>
<Member MemberName="System.Collections.IEnumerable.GetEnumerator">
<MemberSignature Language="C#" Value="System.Collections.IEnumerator IEnumerable.GetEnumerator ();" />
<MemberSignature Language="ILAsm" Value=".method hidebysig newslot virtual instance class System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.System#Collections#IEnumerable#GetEnumerator" />
<MemberSignature Language="VB.NET" Value="Function GetEnumerator () As IEnumerator Implements IEnumerable.GetEnumerator" />
<MemberSignature Language="C++ CLI" Value=" virtual System::Collections::IEnumerator ^ System.Collections.IEnumerable.GetEnumerator() = System::Collections::IEnumerable::GetEnumerator;" />
<MemberType>Method</MemberType>
<Implements>
<InterfaceMember>M:System.Collections.IEnumerable.GetEnumerator</InterfaceMember>
</Implements>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Collections.IEnumerator</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Returns an enumerator that iterates through a collection.</summary>
<returns>An <see cref="T:System.Collections.IEnumerator" /> that can be used to iterate through the collection.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The `foreach` statement of the C# language (`for each` in C++, `For Each` in Visual Basic) hides the complexity of the enumerators. Therefore, using `foreach` is recommended, instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. <xref:System.Collections.IEnumerator.Reset%2A> also brings the enumerator back to this position. At this position, <xref:System.Collections.IEnumerator.Current%2A> is undefined. Therefore, you must call <xref:System.Collections.IEnumerator.MoveNext%2A> to advance the enumerator to the first element of the collection before reading the value of <xref:System.Collections.IEnumerator.Current%2A>.
<xref:System.Collections.IEnumerator.Current%2A> returns the same object until either <xref:System.Collections.IEnumerator.MoveNext%2A> or <xref:System.Collections.IEnumerator.Reset%2A> is called. <xref:System.Collections.IEnumerator.MoveNext%2A> sets <xref:System.Collections.IEnumerator.Current%2A> to the next element.
If <xref:System.Collections.IEnumerator.MoveNext%2A> passes the end of the collection, the enumerator is positioned after the last element in the collection and <xref:System.Collections.IEnumerator.MoveNext%2A> returns `false`. When the enumerator is at this position, subsequent calls to <xref:System.Collections.IEnumerator.MoveNext%2A> also return `false`. If the last call to <xref:System.Collections.IEnumerator.MoveNext%2A> returned `false`, <xref:System.Collections.IEnumerator.Current%2A> is undefined. To set <xref:System.Collections.IEnumerator.Current%2A> to the first element of the collection again, you can call <xref:System.Collections.IEnumerator.Reset%2A> followed by <xref:System.Collections.IEnumerator.MoveNext%2A>.
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 <xref:System.Collections.IEnumerator.MoveNext%2A> or <xref:System.Collections.IEnumerator.Reset%2A> throws an <xref:System.InvalidOperationException>.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Default implementations of collections in <xref:System.Collections.Generic?displayProperty=nameWithType> are not synchronized.
This method is an O(1) operation.
]]></format>
</remarks>
<altmember cref="M:System.Collections.Generic.Stack`1.GetEnumerator" />
<altmember cref="M:System.Collections.Generic.IEnumerable`1.GetEnumerator" />
<altmember cref="T:System.Collections.IEnumerator" />
</Docs>
</Member>
<Member MemberName="ToArray">
<MemberSignature Language="C#" Value="public T[] ToArray ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance !T[] ToArray() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.ToArray" />
<MemberSignature Language="VB.NET" Value="Public Function ToArray () As T()" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; cli::array &lt;T&gt; ^ ToArray();" />
<MemberSignature Language="F#" Value="member this.ToArray : unit -&gt; 'T[]" Usage="stack.ToArray " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>T[]</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Copies the <see cref="T:System.Collections.Generic.Stack`1" /> to a new array.</summary>
<returns>A new array containing copies of the elements of the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to <xref:System.Collections.Generic.Stack%601.Pop%2A>.
This method is an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
## Examples
The following code example demonstrates several methods of the <xref:System.Collections.Generic.Stack%601> generic class, including the <xref:System.Collections.Generic.Stack%601.ToArray%2A> method.
The code example creates a stack of strings with default capacity and uses the <xref:System.Collections.Generic.Stack%601.Push%2A> method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop the first string off the stack. The <xref:System.Collections.Generic.Stack%601.Peek%2A> method is used to look at the next item on the stack, and then the <xref:System.Collections.Generic.Stack%601.Pop%2A> method is used to pop it off.
The <xref:System.Collections.Generic.Stack%601.ToArray%2A> method is used to create an array and copy the stack elements to it, then the array is passed to the <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor that takes <xref:System.Collections.Generic.IEnumerable%601>, creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
An array twice the size of the stack is created, and the <xref:System.Collections.Generic.Stack%601.CopyTo%2A> method is used to copy the array elements beginning at the middle of the array. The <xref:System.Collections.Generic.Stack%601.%23ctor%2A> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
[!code-csharp[Generic.Stack#1](~/samples/snippets/csharp/VS_Snippets_CLR/Generic.Stack/cs/source.cs#1)]
[!code-vb[Generic.Stack#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb#1)]
]]></format>
</remarks>
<altmember cref="M:System.Collections.Generic.Stack`1.CopyTo(`0[],System.Int32)" />
<altmember cref="M:System.Collections.Generic.Stack`1.Pop" />
</Docs>
</Member>
<Member MemberName="TrimExcess">
<MemberSignature Language="C#" Value="public void TrimExcess ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance void TrimExcess() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.TrimExcess" />
<MemberSignature Language="VB.NET" Value="Public Sub TrimExcess ()" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; void TrimExcess();" />
<MemberSignature Language="F#" Value="member this.TrimExcess : unit -&gt; unit" Usage="stack.TrimExcess " />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Sets the capacity to the actual number of elements in the <see cref="T:System.Collections.Generic.Stack`1" />, if that number is less than 90 percent of current capacity.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This method can be used to minimize a collection's memory overhead if no new elements will be added to the collection. The cost of reallocating and copying a large <xref:System.Collections.Generic.Stack%601> can be considerable, however, so the <xref:System.Collections.Generic.Stack%601.TrimExcess%2A> method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain.
This method is an O(`n`) operation, where `n` is <xref:System.Collections.Generic.Stack%601.Count%2A>.
To reset a <xref:System.Collections.Generic.Stack%601> to its initial state, call the <xref:System.Collections.Generic.Stack%601.Clear%2A> method before calling <xref:System.Collections.Generic.Stack%601.TrimExcess%2A> method. Trimming an empty <xref:System.Collections.Generic.Stack%601> sets the capacity of the <xref:System.Collections.Generic.Stack%601> to the default capacity.
]]></format>
</remarks>
<altmember cref="M:System.Collections.Generic.Stack`1.Clear" />
<altmember cref="P:System.Collections.Generic.Stack`1.Count" />
</Docs>
</Member>
<Member MemberName="TryPeek">
<MemberSignature Language="C#" Value="public bool TryPeek (out T result);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance bool TryPeek([out] !T&amp; result) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.TryPeek(`0@)" />
<MemberSignature Language="VB.NET" Value="Public Function TryPeek (ByRef result As T) As Boolean" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; bool TryPeek([Runtime::InteropServices::Out] T % result);" />
<MemberSignature Language="F#" Value="member this.TryPeek : -&gt; bool" Usage="stack.TryPeek result" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="result" Type="T" RefType="out" Index="0" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netstandard-2.1" />
</Parameters>
<Docs>
<param name="result">To be added.</param>
<summary>To be added.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="TryPop">
<MemberSignature Language="C#" Value="public bool TryPop (out T result);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig instance bool TryPop([out] !T&amp; result) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.Stack`1.TryPop(`0@)" />
<MemberSignature Language="VB.NET" Value="Public Function TryPop (ByRef result As T) As Boolean" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; bool TryPop([Runtime::InteropServices::Out] T % result);" />
<MemberSignature Language="F#" Value="member this.TryPop : -&gt; bool" Usage="stack.TryPop result" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Collections</AssemblyName>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System</AssemblyName>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="result" Type="T" RefType="out" Index="0" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netstandard-2.1" />
</Parameters>
<Docs>
<param name="result">To be added.</param>
<summary>To be added.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
</Members>
</Type>
You can’t perform that action at this time.