C#  

List, Dictionary, Queue, and Stack

Concept Overview

Collections in C# are used to store, manage, and manipulate groups of objects.
Commonly used collections are:

  • List – Stores a list of items (dynamic array)

  • Dictionary<TKey, TValue> – Stores key–value pairs

  • Queue – Follows First-In-First-Out (FIFO)

  • Stack – Follows Last-In-First-Out (LIFO)

Real-Time Example Scenario: “Online Order Processing System”

Imagine an e-commerce web application where:

  • Admin wants to see a list of product names (List)

  • We store Product ID & Name pairs (Dictionary)

  • We handle Customer Orders in order of arrival (Queue)

  • We maintain User navigation actions (Stack) for backtracking

ASP.NET Web Forms Example

File: CollectionsDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CollectionsDemo.aspx.cs" Inherits="WebApp.CollectionsDemo" %>

<!DOCTYPE html>
<html>
<head>
    <title>Collections Example - ASP.NET WebForms</title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Working with Collections in C# (ASP.NET WebForms)</h2>

        <asp:Button ID="btnShowCollections" runat="server" Text="Show Collections Example" OnClick="btnShowCollections_Click" />
        <hr />

        <asp:Label ID="lblOutput" runat="server" Text="" />
    </form>
</body>
</html>

File: CollectionsDemo.aspx.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WebApp
{
    public partial class CollectionsDemo : System.Web.UI.Page
    {
        protected void btnShowCollections_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            // -------------------------
            // 1. List Example - Products
            // -------------------------
            List<string> productList = new List<string> { "Laptop", "Mouse", "Keyboard", "Monitor" };
            sb.Append("<b>List Example - Product List:</b><br/>");
            foreach (var product in productList)
                sb.Append(product + "<br/>");

            sb.Append("<hr/>");

            // -------------------------
            // 2. Dictionary Example - Product IDs
            // -------------------------
            Dictionary<int, string> productDict = new Dictionary<int, string>
            {
                {101, "Laptop"},
                {102, "Mouse"},
                {103, "Keyboard"},
                {104, "Monitor"}
            };
            sb.Append("<b>Dictionary Example - Product ID & Name:</b><br/>");
            foreach (var kvp in productDict)
                sb.Append($"ID: {kvp.Key}, Name: {kvp.Value}<br/>");

            sb.Append("<hr/>");

            // -------------------------
            // 3. Queue Example - Order Processing
            // -------------------------
            Queue<string> orderQueue = new Queue<string>();
            orderQueue.Enqueue("Order#1001");
            orderQueue.Enqueue("Order#1002");
            orderQueue.Enqueue("Order#1003");

            sb.Append("<b>Queue Example - Orders in FIFO:</b><br/>");
            while (orderQueue.Count > 0)
                sb.Append(orderQueue.Dequeue() + " Processed<br/>");

            sb.Append("<hr/>");

            // -------------------------
            // 4. Stack Example - User Navigation
            // -------------------------
            Stack<string> navigationStack = new Stack<string>();
            navigationStack.Push("Home Page");
            navigationStack.Push("Product Page");
            navigationStack.Push("Checkout Page");

            sb.Append("<b>Stack Example - Navigation Back History (LIFO):</b><br/>");
            while (navigationStack.Count > 0)
                sb.Append("Back to: " + navigationStack.Pop() + "<br/>");

            // Output to Label
            lblOutput.Text = sb.ToString();
        }
    }
}

Output

When you click “Show Collections Example”, the browser displays:

List Example - Product List:
Laptop
Mouse
Keyboard
Monitor
----------------------------
Dictionary Example - Product ID & Name:
ID: 101, Name: Laptop
ID: 102, Name: Mouse
...
----------------------------
Queue Example - Orders in FIFO:
Order#1001 Processed
Order#1002 Processed
Order#1003 Processed
----------------------------
Stack Example - Navigation Back History (LIFO):
Back to: Checkout Page
Back to: Product Page
Back to: Home Page

Real-Time Use Cases

Collection TypeReal-Time Example in ASP.NET
ListStore all product names fetched from database
DictionaryStore product ID and details for quick lookup
QueueManage customer service ticket processing
StackHandle page navigation or undo-redo actions