Você está na página 1de 5

Stack Methods

Name Description

Removes all objects from the Stack.


Clear()

Clone() Creates a shallow copy of the Stack.

Contains(Object) Determines whether an element is in the Stack.

CopyTo(Array, Int32) Copies the Stack to an existing one-dimensional Array,


starting at the specified array index.

Equals(Object) Determines whether the specified object is equal to the


current object.(Inherited from Object.)

Finalize() Allows an object to try to free resources and perform other


cleanup operations before it is reclaimed by garbage
collection.(Inherited from Object.)

GetEnumerator() Returns an IEnumerator for the Stack.

GetHashCode() Serves as the default hash function. (Inherited from Object.)

GetType() Gets the Type of the current instance.(Inherited from Object.)

MemberwiseClone() Creates a shallow copy of the current Object.(Inherited


from Object.)

Peek() Returns the object at the top of the Stack without removing it.

Pop() Removes and returns the object at the top of the Stack.

Push(Object) Inserts an object at the top of the Stack.

Synchronized(Stack) Returns a synchronized (thread safe) wrapper for the Stack.

ToArray() Copies the Stack to a new array.


ToString() Returns a string that represents the current object.(Inherited
from Object.)

The following example shows how to create and add values to a Stack and how to display its
values.
C#
C++
VB
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*/
The following example shows how to add elements to the Stack, remove elements from
the Stack, or view the element at the top of the Stack.
C#
C++
VB
using System;
using System.Collections;
public class SamplesStack {

public static void Main() {

// Creates and initializes a new Stack.


Stack myStack = new Stack();
myStack.Push( "The" );
myStack.Push( "quick" );
myStack.Push( "brown" );
myStack.Push( "fox" );

// Displays the Stack.


Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );

// Removes an element from the Stack.


Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );

// Displays the Stack.


Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );

// Removes another element from the Stack.


Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );

// Displays the Stack.


Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );

// Views the first element in the Stack but does not remove it.
Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );

// Displays the Stack.


Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );
}

public static void PrintValues( IEnumerable myCollection, char


mySeparator ) {
foreach ( Object obj in myCollection )
Console.Write( "{0}{1}", mySeparator, obj );
Console.WriteLine();
}
}

/*
This code produces the following output.

Stack values: fox brown quick The


(Pop) fox
Stack values: brown quick The
(Pop) brown
Stack values: quick The
(Peek) quick
Stack values: quick The
*/

The following example shows how to clear the values of the Stack.
C#
C++
VB
using System;
using System.Collections;

public class SamplesStack {

public static void Main() {

// Creates and initializes a new Stack.


Stack myStack = new Stack();
myStack.Push( "The" );
myStack.Push( "quick" );
myStack.Push( "brown" );
myStack.Push( "fox" );
myStack.Push( "jumped" );

// Displays the count and values of the Stack.


Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", myStack.Count );
Console.Write( " Values:" );
PrintValues( myStack );

// Clears the Stack.


myStack.Clear();

// Displays the count and values of the Stack.


Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", myStack.Count );
Console.Write( " Values:" );
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.

Initially,
Count : 5
Values: jumped fox brown quick The
After Clear,
Count : 0
Values:
*/

Você também pode gostar