07 Feb 2024




Intermediate

In C#, the generic equivalents for ArrayList, Stack, Queue, and Hashtable are provided by the following classes:

  • ArrayList -> List<T>
  • Stack -> Stack<T>
  • Queue -> Queue<T>
  • Hashtable -> Dictionary<TKey, TValue>
  1. ArrayList: The generic equivalent of ArrayList in C# is the List<T> class, where T is the type of elements stored in the list. It is found in the System.Collections.Generic namespace.

    • List<T> (Equivalent of ArrayList):
      // Creating a List of integers
      List<int> numbers = new List<int>();
      
      // Adding elements to the list
      numbers.Add(1);
      numbers.Add(2);
      numbers.Add(3);
      
      // Accessing elements
      foreach (var num in numbers)
      {
          Console.WriteLine(num);
      }
      
  2. Stack: The generic equivalent of Stack in C# is the Stack<T> class, where T is the type of elements stored in the stack. It is also found in the System.Collections.Generic namespace.

    • Stack<T> (Equivalent of Stack):
      // Creating a stack of strings
      Stack<string> stackOfStrings = new Stack<string>();
      
      // Pushing elements onto the stack
      stackOfStrings.Push("First");
      stackOfStrings.Push("Second");
      stackOfStrings.Push("Third");
      
      // Popping elements from the stack
      while (stackOfStrings.Count > 0)
      {
          string poppedElement = stackOfStrings.Pop();
          Console.WriteLine($"Popped: {poppedElement}");
      }
      
  3. Queue: The generic equivalent of Queue in C# is the Queue<T> class, where T is the type of elements stored in the queue. It is also found in the System.Collections.Generic namespace.

    • Queue<T> (Equivalent of Queue):
      // Creating a queue of doubles
      Queue<double> doubleQueue = new Queue<double>();
      
      // Enqueuing elements into the queue
      doubleQueue.Enqueue(1.5);
      doubleQueue.Enqueue(2.5);
      doubleQueue.Enqueue(3.5);
      
      // Dequeuing elements from the queue
      while (doubleQueue.Count > 0)
      {
          double dequeuedElement = doubleQueue.Dequeue();
          Console.WriteLine($"Dequeued: {dequeuedElement}");
      }
      
  4. Hashtable: The generic equivalent of Hashtable in C# is the Dictionary<TKey, TValue> class, where TKey is the type of keys and TValue is the type of values stored in the dictionary. It is found in the System.Collections.Generic namespace.

    • Dictionary<TKey, TValue> (Equivalent of Hashtable):
      // Creating a dictionary with string keys and int values
      Dictionary<string, int> ageDictionary = new Dictionary<string, int>();
      
      // Adding key-value pairs to the dictionary
      ageDictionary["Alice"] = 25;
      ageDictionary["Bob"] = 30;
      ageDictionary["Charlie"] = 22;
      
      // Accessing values using keys
      Console.WriteLine("Age of Bob: " + ageDictionary["Bob"]);
      

Here's a brief summary of each:

  • List<T>: Provides a resizable array-like data structure that stores elements of a specified type T.

  • Stack<T>: Represents a last-in, first-out (LIFO) collection of elements of a specified type T.

  • Queue<T>: Represents a first-in, first-out (FIFO) collection of elements of a specified type T.

  • Dictionary<TKey, TValue>: Represents a collection of key-value pairs where each key must be unique.

These generic collections provide type safety and better performance compared to their non-generic counterparts.

c-sharp
collections