Stack Tutorial In C#

CK CK 3788 
Created at
Updated at  
674 0 0

stack is a LIFO (Last In First Out) queue, so if you add two items and then ask for an item you would get the second item then the first item.

To add items to the Stack you use Push method. This method takes an object of any type but to keep things simple lets just give it a string object:

Stack s = new Stack();

s.Push("So");
s.Push("This");
s.Push("Is");
s.Push("How");
s.Push("Queues");
s.Push("Work");

Viewing a Single Object Without Removing it From the Stack

You can use the Peak method to get the bottom-most object in the Stack without removing it from the Stack:

Console.WriteLine(s.Peek());


Viewing All the Objects in the Stack Without Removing Them

You can read any of the data in the Stack by using an enumerator: 

System.Collections.IEnumerator en = s.GetEnumerator();

while (en.MoveNext()) 
{
   Console.Write(en.Current   " ");
}


Removing Items from the Stack

To pop an item from the Stack you can use the Pop statement. This returns the bottom-most object of the stack. 
 

while( s.Count > 0)
{
   Console.Write(s.Pop()   " ");
}

 

Tags C# Stack LIFO Pop Push Stack Facebook X
Comments 0
Similar posts
  1. Rust Std library types
    370
  2. How do I implement a stack in Rust?
    769
  1. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  2. A Customizable Printing Text Class
    439
  3. Simplified .NET Printing In C#
    696
  4. How To Print Text In C#
    456
  5. C# Lists
    1,162
  6. Simple Example of IF/THEN using C#
    1,270
  7. Fast and Good Keyboard/Mouse Test without the message handler
    984
  8. Simple CGI Programming in C
    550
  9. C++ CGI Example working on Apache
    490
  10. Simple C# CGI working on Apache
    486
  11. How to send binary data through C# CGI app?
    998
  12. Interacting With TinyPic From C#
    1,314
  13. Creating A Watched Folder With Assigned Events
    1,672
  14. Calling Your Main Thread From A Worker Thread In C#
    551
  15. HashTable Tutorial In C#
    1,794
  16. Queue Tutorial In C#
    1,753
  17. Create PDF Files On the Fly in C#
    1,008
  18. How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport
    1,174
  19. Send Email Using C#
    1,381
  20. How To Read And Write A Cookie
    365
  21. Using C# With Cookies
    629
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    29
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  4. How do I create animated background for Google Meet?
    22,863
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  8. Array of arguments passed to script in PHP
    97
  9. Invalid Auction Storms in Ad Tech
    106
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  11. The Two Clocks of Ad Server Architecture
    77
  12. Frequency Cap Architecture Overview
    526
  13. Collecting Proof-of-Play from 100,000 Screens
    152
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  15. What is RedisVL?
    114
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  17. TPU & GPU Cluster Management Tools
    257
  18. The Concept of Scaffolding in AI Agents
    244
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  23. Machine Learning: Training vs. Ranking
    229
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    671
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    200
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232