Stack 类

表示对象的简单的后进先出非泛型集合。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class Stack
    Implements ICollection, IEnumerable, ICloneable
用法
Dim instance As Stack
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class Stack : ICollection, IEnumerable, ICloneable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Stack : ICollection, IEnumerable, ICloneable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class Stack implements ICollection, IEnumerable, 
    ICloneable
SerializableAttribute 
ComVisibleAttribute(true) 
public class Stack implements ICollection, IEnumerable, 
    ICloneable

备注

有关此集合的泛型版本,请参见 System.Collections.Generic.Stack

Stack 实现为循环缓冲区。

Stack 的容量是 Stack 可以保存的元素数。Stack 的默认初始容量为 10。向 Stack 添加元素时,将通过重新分配来根据需要自动增大容量。

如果 Count 小于堆栈的容量,则 Push 为 O(1) 操作。如果需要增加容量以容纳新元素,则 Push 成为 O(n) 操作,其中 n 为 CountPop 为 O(1) 操作。

Stack 接受 空引用(在 Visual Basic 中为 Nothing) 作为有效值并且允许重复的元素。

示例

下面的示例说明如何创建 Stack 并向其添加值,以及如何打印出其值。

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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 'PrintValues

End Class

' 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
*/ 
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
 */
import System.*;
import System.Collections.*;

public class SamplesStack
{
    public static void main(String[] args)
    {
        // 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}", 
            System.Convert.ToString(myStack.get_Count()));
        Console.Write("\tValues:");
        PrintValues(myStack);
    } //main

    public static void PrintValues(IEnumerable myCollection)
    {
        IEnumerator objEnum = myCollection.GetEnumerator();

        while (objEnum.MoveNext()) {
            Console.Write("    {0}", objEnum.get_Current());
        }
        Console.WriteLine();
    } //PrintValues

} //SamplesStack

/* 
 This code produces the following output.
 
 myStack
     Count:    3
     Values:    !    World    Hello
 */

继承层次结构

System.Object
  System.Collections.Stack
     Microsoft.VisualC.SymbolTableStack

线程安全

此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。

若要保证 Stack 的线程安全,则必须通过由 Synchronized 方法返回的包装来执行所有操作。

通过集合枚举在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Stack 成员
System.Collections 命名空间
System.Collections.Generic.Stack