Stack 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对象的简单后进先出 (LIFO) 非泛型集合。
public ref class Stack : System::Collections::ICollectionpublic ref class Stack : ICloneable, System::Collections::ICollectionpublic class Stack : System.Collections.ICollectionpublic class Stack : ICloneable, System.Collections.ICollection[System.Serializable]
public class Stack : ICloneable, System.Collections.ICollection[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Stack : ICloneable, System.Collections.ICollectiontype Stack = class
    interface ICollection
    interface IEnumerabletype Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneable[<System.Serializable>]
type Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneable[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneablePublic Class Stack
Implements ICollectionPublic Class Stack
Implements ICloneable, ICollection- 继承
- 
				Stack
- 属性
- 实现
示例
以下示例演示如何创建和向 Stack 添加值,以及如何显示其值。
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{
   
   // Creates and initializes a new Stack.
   Stack^ myStack = gcnew 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 );
}
void PrintValues( IEnumerable^ myCollection )
{
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );
   }
   Console::WriteLine();
}
/* 
 This code produces the following output.
 
 myStack
     Count:    3
     Values:    !    World    Hello
 */
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 )  {
      foreach ( Object obj in myCollection )
         Console.Write( "    {0}", obj );
      Console.WriteLine();
   }
}
/*
This code produces the following output.
myStack
    Count:    3
    Values:    !    World    Hello
*/
Imports System.Collections
Public Class SamplesStack    
    
    Public Shared Sub Main()
    
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("Hello")
        myStack.Push("World")
        myStack.Push("!")
        
        ' Displays the properties and values of the Stack.
        Console.WriteLine("myStack")
        Console.WriteLine(ControlChars.Tab & "Count:    {0}", myStack.Count)
        Console.Write(ControlChars.Tab & "Values:")
        PrintValues(myStack)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub
End Class
' This code produces the following output.
'
' myStack
'     Count:     3
'     Values:    !    World    Hello
注解
的 Stack 容量是 可以容纳的元素 Stack 数。 将元素添加到 时 Stack,容量会根据需要通过重新分配自动增加。
重要
不建议使用 Stack 类进行新的开发。 建议改用泛型 System.Collections.Generic.Stack<T> 类。 有关详细信息,请参阅 GitHub 上 不应使用非泛型集合 。
如果 Count 小于堆栈的容量, Push 是一个 O(1) 操作。 如果需要增加容量以适应新元素, Push 将成为一个 O(n) 操作,其中 n 为 Count。 
              Pop 是一个 O(1) 操作。
              Stack 接受 null 作为有效值,并允许重复元素。
构造函数
| Stack() | 初始化 Stack 类的新实例,该实例为空并且具有默认初始容量。 | 
| Stack(ICollection) | 初始化 Stack 类的新实例,该实例包含从指定集合复制的元素并且具有与所复制的元素数相同的初始容量。 | 
| Stack(Int32) | 初始化 Stack 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。 | 
属性
| Count | 获取 Stack 中包含的元素数。 | 
| IsSynchronized | 获取一个值,该值指示是否同步对 Stack 的访问(线程安全)。 | 
| SyncRoot | 获取可用于同步对 Stack 的访问的对象。 | 
方法
| Clear() | 从 Stack 中移除所有对象。 | 
| Clone() | 创建 Stack 的浅表副本。 | 
| Contains(Object) | 确定某元素是否在 Stack 中。 | 
| CopyTo(Array, Int32) | |
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetEnumerator() | 返回 IEnumerator 的 Stack。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Peek() | 返回位于 Stack 顶部的对象但不将其移除。 | 
| Pop() | 删除并返回 Stack 顶部的对象。 | 
| Push(Object) | 在 Stack 的顶部插入一个对象。 | 
| Synchronized(Stack) | 返回 Stack 的同步(线程安全)包装。 | 
| ToArray() | 将 Stack 复制到新数组中。 | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 | 
适用于
线程安全性
Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
若要保证 的 Stack线程安全,所有操作都必须通过 方法返回的 Synchronized(Stack) 包装器完成。
枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。