Stack 類別

定義

表示物件非泛型集合的簡單先出 (LIFO) 。

public ref class Stack : System::Collections::ICollection
public ref class Stack : ICloneable, System::Collections::ICollection
public class Stack : System.Collections.ICollection
public 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.ICollection
type Stack = class
    interface ICollection
    interface IEnumerable
type 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 ICloneable
Public Class Stack
Implements ICollection
Public Class Stack
Implements ICloneable, ICollection
繼承
Stack
屬性
實作

範例

以下範例說明如何建立並新增堆疊值,以及如何顯示其值。

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

備註

a Stack 的容量是它 Stack 能容納的元素數量。 當元素新增至 Stack時,容量會視需要透過重新配置自動增加。

Important

不建議您將 Stack 類別用於新的開發。 相反地,我們建議您使用泛型 System.Collections.Generic.Stack<T> 類別。 如需詳細資訊,請參閱 gitHub 上不應 使用非泛型集合

Count 小於堆疊容量,則 Push 為操作 O(1) 。 若容量需增加以容納新元素, Push 則為運算, O(n) 其中 nCountPop 是一個 O(1) 操作。

Stack 接受 null 為有效值,並允許重複元素。

建構函式

名稱 Description
Stack()

初始化空的 Stack 類別的新實例,並具有預設的初始容量。

Stack(ICollection)

初始化一個包含從指定集合複製的元素,且初始容量與複製元素數量相同的類別實例 Stack

Stack(Int32)

初始化一個空的類別新實例,該實例 Stack 為空且擁有指定的初始容量或預設初始容量,以較大者為準。

屬性

名稱 Description
Count

得到包含於 的 Stack元素數量。

IsSynchronized

取得值,指出是否同步存取 Stack (線程安全)。

SyncRoot

取得一個物件,可用來同步存取 Stack

方法

名稱 Description
Clear()

移除所有物件。Stack

Clone()

建立 Stack的淺層複本。

Contains(Object)

判斷專案是否在 Stack中。

CopyTo(Array, Int32)

從指定的陣列索引開始,將 複製 Stack 到 一維 Array

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)

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於

執行緒安全性

此類型的公用靜態 (Shared) 成員是安全線程。 任何實例成員都不保證具有執行緒安全性。

若要保證 Stack的線程安全性,所有作業都必須透過 Synchronized(Stack) 方法傳回的包裝函式來完成。

透過集合列舉本質上不是安全線程的程式。 即使集合同步處理,其他線程仍然可以修改集合,這會導致列舉值擲回例外狀況。 若要保證列舉期間的線程安全性,您可以在整個列舉期間鎖定集合,或攔截其他線程所做的變更所產生的例外狀況。

另請參閱