Assembly : mscorlib (dans mscorlib.dll)
Syntaxe<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
NotesPour la version générique de cette collection, consultez System.Collections.Generic.Stack.
Stack est implémenté sous forme de mémoire tampon circulaire.
La capacité de Stack correspond au nombre d'éléments que peut contenir Stack. La capacité initiale par défaut de Stack est 10. Lorsque des éléments sont ajoutés à Stack, la capacité augmente automatiquement par réallocation.
Si Count est inférieur à la capacité de la pile, Push est une opération O(1). Si la capacité doit être augmentée pour intégrer le nouvel élément, Push devient une opération O(n), où n est égal à Count. Pop est une opération O(1).
Stack accepte référence Null (Nothing en Visual Basic) comme valeur valide et autorise les éléments en double.
ExempleL'exemple suivant illustre la création et l'ajout de valeurs à Stack et l'impression de ses valeurs.
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 */
Sécurité des threadsLes membres statiques publics (Shared en Visual Basic) de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.
Afin de garantir la sécurité des threads de Stack, toutes les opérations doivent être effectuées par l'intermédiaire du wrapper retourné par la méthode Synchronized.
L'énumération d'une collection n'est intrinsèquement pas une procédure thread-safe. Même lorsqu'une collection est synchronisée, les autres threads peuvent toujours modifier la collection, ce qui entraîne la levée d'une exception par l'énumérateur. Pour garantir la sécurité des threads au cours de l'énumération, vous pouvez soit verrouiller la collection pendant toute l'énumération, soit intercepter les exceptions résultant des modifications apportées par les autres threads.
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ