Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Stack

C# Stack

C# Collection C# Collections

In C#, a stack (Stack) is a last-in, first-out (LIFO, Last In First Out) data structure.

A stack (Stack) is suitable for storing and processing data in order, where the most recently added element is removed first.

A stack (Stack) represents alast-in-first-outcollection of objects. Use a stack when you need last-in, first-out access to items. When you add an item to the list, it is calledpushingan element, and when you remove an item from the list, it is calledpopping.

Stack provides two implementations:

  1. Non-generic Stack(System.Collections.Stack): supports storing objects of any type (requires boxing and unboxing operations).
  2. Generics Stack(System.Collections.Generic.Stack): supports strongly typed objects, avoiding boxing and unboxing and improving performance.

** Stack Features:**

Stack Class Methods and Properties

The following table lists the Stack Some commonly used members of the class property:

Attribute name Type Description
Count int Gets the number of elements in the stack.
SyncRoot object Gets an object used to synchronize access to the stack (non-generic).
IsSynchronized bool Indicates whether access to the stack is synchronized (thread-safe, always false)。

td>Gets the number of elements contained in the Stack.

The following table lists the Stack Some commonly used members of the class Method:

Method Name Return Type Description
Element Operations
Push(object item) void Pushes an element onto the top of the stack.
Pop() object Removes and returns the element at the top of the stack.
Peek() object Returns the element at the top of the stack without removing it.
Clear() void Removes all elements from the stack.
Checking and Copying
Contains(object item) bool Determines whether an element is in the stack.
ToArray() object[] Copies the elements of the stack to a new array (in reversed order).
Clone() object Creates a shallow copy of the current stack.
CopyTo(Array array, int index) void Copies the elements of the stack to an existing array, starting at the specified index.
Enumerator Support
GetEnumerator() IEnumerator Returns an enumerator that iterates through the elements of the stack.
Thread Safety
Synchronized(Stack stack) Stack Returns a thread-safe wrapper around the stack.

Example

The following example demonstrates the use of a stack (Stack).

Non-generic stack:

Example

using

System

;

using

System.Collections

;

namespace

CollectionsApplication

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

            Stack st

=

new

Stack

(

)

;

            st

.

Push

(

'A'

)

;

            st

.

Push

(

'M'

)

;

            st

.

Push

(

'G'

)

;

            st

.

Push

(

'W'

)

;

           

            Console

.

WriteLine

(

"Current stack: "

)

;

           

foreach

(

char

c

in

st

)

           

{

                Console

.

Write

(

c

" "

)

;

           

}

            Console

.

WriteLine

(

)

;

           

            st

.

Push

(

'V'

)

;

            st

.

Push

(

'H'

)

;

            Console

.

WriteLine

(

"The next poppable value in stack: {0}"

,

            st

.

Peek

(

)

)

;

            Console

.

WriteLine

(

"Current stack: "

)

;

         

           

foreach

(

char

c

in

st

)

           

{

               Console

.

Write

(

c

" "

)

;

           

}

            Console

.

WriteLine

(

)

;

            Console

.

WriteLine

(

"Removing values "

)

;

            st

.

Pop

(

)

;

            st

.

Pop

(

)

;

            st

.

Pop

(

)

;

           

            Console

.

WriteLine

(

"Current stack: "

)

;

           

foreach

(

char

c

in

st

)

           

{

               Console

.

Write

(

c

" "

)

;

           

}

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:


Current stack: 
W G M A
The next poppable value in stack: H
Current stack: 
H V W G M A
Removing values
Current stack: 
G M A

Generic stack:

Example

using

System

;

using

System.Collections.Generic

;

class

Program

{

   

static

void

Main

(

)

   

{

        Stack

<
int
stack
=
new
Stack
<
int
(
)

;

       

// Push onto stack

        stack

.

Push

(

10

)

;

        stack

.

Push

(

20

)

;

        stack

.

Push

(

30

)

;

       

// View the top of the stack

        Console

.

WriteLine

(

$

"Peek: {stack.Peek()}"

)

;

// Output: 30

       

// Pop from stack

        Console

.

WriteLine

(

$

"Pop: {stack.Pop()}"

)

;

// Output: 30

       

// Remaining stack

        Console

.

WriteLine

(

"Remaining items:"

)

;

       

foreach

(

var

item

in

stack

)

       

{

            Console

.

WriteLine

(

item

)

;

// Output: 20, 10

       

}

   

}

}

Stack vs. Stack Difference

Stack (non-generic)

Stack(generic)

C# Collection C# Collections