Stack 类

定义

表示对象非泛型集合的简单最后一出(LIFO)。

public ref class Stack : System::Collections::ICollection
public ref class Stack : ICloneable, System::Collections::ICollection
public class Stack : 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
public class Stack : ICloneable, System.Collections.ICollection
type Stack = class
    interface ICollection
    interface IEnumerable
[<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
type Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
Public Class Stack
Implements ICollection
Public Class Stack
Implements ICloneable, ICollection
继承
Stack
属性
实现

示例

以下示例演示如何创建值并将其添加到 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

注解

一个 Stack 元素的容量是可以保留的元素 Stack 数。 随着元素添加到 a Stack,容量通过重新分配自动增加。

Important

不建议将 Stack 类用于新开发。 相反,我们建议使用泛型 System.Collections.Generic.Stack<T> 类。 有关详细信息,请参阅不应在 GitHub 上使用 非泛型集合

如果 Count 小于堆栈的容量, Push 则为 O(1) 操作。 如果需要增加容量以容纳新元素,Push则变为一个O(n)操作,其中nCount 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)

从指定的数组索引开始, 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)

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于

线程安全性

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

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

通过集合进行枚举本质上不是线程安全的过程。 即使集合同步,其他线程仍可以修改集合,这会导致枚举器引发异常。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合,也可以捕获由其他线程所做的更改导致的异常。

另请参阅