07 Feb 2024
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>
-
ArrayList: The generic equivalent of ArrayList in C# is the
List<T>class, whereTis the type of elements stored in the list. It is found in theSystem.Collections.Genericnamespace.- 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); }
- List<T> (Equivalent of ArrayList):
-
Stack: The generic equivalent of Stack in C# is the
Stack<T>class, whereTis the type of elements stored in the stack. It is also found in theSystem.Collections.Genericnamespace.- 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}"); }
- Stack<T> (Equivalent of Stack):
-
Queue: The generic equivalent of Queue in C# is the
Queue<T>class, whereTis the type of elements stored in the queue. It is also found in theSystem.Collections.Genericnamespace.- 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}"); }
- Queue<T> (Equivalent of Queue):
-
Hashtable: The generic equivalent of Hashtable in C# is the
Dictionary<TKey, TValue>class, whereTKeyis the type of keys andTValueis the type of values stored in the dictionary. It is found in theSystem.Collections.Genericnamespace.- 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"]);
- Dictionary<TKey, TValue> (Equivalent of Hashtable):
Here's a brief summary of each:
-
List<T>: Provides a resizable array-like data structure that stores elements of a specified typeT. -
Stack<T>: Represents a last-in, first-out (LIFO) collection of elements of a specified typeT. -
Queue<T>: Represents a first-in, first-out (FIFO) collection of elements of a specified typeT. -
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.