Lists, Stacks, Queues, Dictionaries c#
Similar to the array data type in the C# programming language, C# also provides its users with several powerful collection data type choices. These are built-in features of the System.Collections.Generic namespace in the C# library. These collection data types include List<T>, Stack<T>, Queue<T>, and Dictionary<TKey, TValue>. These collection data types offer C# users greater flexibility and reliability than array data types, and provide dynamic data storage features with methods for adding new data, deleting existing data, and manually controlling collection data manipulation.

So, let’s take a closer look at the List<T>, Stack<T>, Queue<T>, and Dictionary<TKey, TValue> collection data types and their uses in C# programming.
List<T> Collection Data Type in C#.
In C# programming, Collection is a List<T> generic data type method. It provides C# users with the ability to store elements of any collection data type on disk. Unlike the array data type, the List collection data type can have its size modified dynamically when a new element is added or deleted. When a C# user needs an ordered, dynamic list of the latest updated items in a collection group, the List<T> collection data type is the most commonly used collection method or type.
Creating a List Collection Data Type in C#.
using System;
using System.Collections.Generic;
class ListExample
{
static void Main()
{
// Here we are creating a list of numeric integers
List<int> integers = new List<int>();
// Here we are adding a number of list elements
integers.Add(9);
integers.Add(8);
integers.Add(7);
integers.Add(6);
// here we Initializing a List with text values
List<string> laptops = new List<string> { “Apple Macbook Pro”, “Hp Pavilion”, “Dell Vostro” , “Microsoft surface”};
// here we Output the list of integer elements
foreach (var integers in integers)
{
Console.WriteLine(intgrs); // Result is – 9 8 7 6
}
foreach (var laptop in laptops)
{
Console.WriteLine(laptop); // Result is – Apple Macbook Pro Hp Pavilion Dell Vostro Microsoft surface
}
}
}
Special features of the C# List<T> Collection Data Type.
- Add(T item) – It adds a new item to the existing list collection data type.
- Remove(T item) – It removes or deletes an item from the existing list collection data type.
- Insert(int index, T item) – It inserts an item at a specified index location in the existing list collection data type.
- Contains(T item) – It checks whether the list contains an item in the existing list collection data type.
- Count – It finds the element number in the list in the existing list collection data type.
- RemoveAt(int index) – It removes the element at the indicated index location in the existing list collection data type.
- Clear() – It clears all items from the list in the existing list collection data type.
Stack<T> Collection Data Type in C#.
In C# programming, Stack<T> is a collection data type method that allows the Last In, First Out (LIFO) object element push concept or principle to a secondary storage location on disk. The use of the C# Stack<T> collection is important when a C# user needs to access or retrieve data elements stored in the Stack<T> data type in the reverse order of the order in which they were added, for example, a stack of plates, with the last plate considered the first.
Create a Stack Collection Data Type in C#.
using System;
using System.Collections.Generic;
class StackTExample
{
static void Main()
{
//Here we are creating a stack of numeric integers
Stack<int> stack = new Stack<int>();
// Here we are pushing numeric items onto the stack in order
stack.Push(90);
stack.Push(80);
stack.Push(70);
stack.Push(60);
// Here we are popping an item off the stack; it removes the top of the stack element
int topElement = stack.Pop();
Console.WriteLine(“Popped stack element – ” + topElement); // Result is – 60
// here we Peeking at the top element without removing it in stack order
Console.WriteLine(“Top stack element – ” + stack.Peek()); // Result is – 70
// here we Checking if the stack contains a specific stack element
Console.WriteLine(“Contains stack element 90 – ” + stack.Contains(90)); // Result is – True
}
}
Special features of the C# Stack<T> Collection Data Type.
- Push(T item) – It adds a new item to the top of the stack in the existing Stack collection data type.
- Pop() – It removes the top item from the stack in the existing Stack collection data type and returns it.
- Peek() – It returns the top item from the stack in the existing Stack collection data type without removing it.
- Contains(T item) – It checks the stack in the existing Stack collection data type to see if there is an item in the stack.
- Clear() – It removes all the elements stored in the stack in the existing Stack collection data type.
Queue<T> Collection Data Type in C#.
In C# programming, Queue<T> is a collection data type that follows the First In, First Out (FIFO) concept method or principal order. The Queue<T> data type is used in C# programs when users want to process or retrieve Queue<T> data element items in the same storage order in which they were added to the Queue<T> data type. For example, when managing a printing task or an event in the system, etc.
Create a Queue Collection Data Type in C#.
using System;
using System.Collections.Generic;
class QueueExample
{
static void Main()
{
// Here we are creating a Queue of integers.
Queue<int> queue = new Queue<int>();
// Here we are enqueuing Queue items by simply adding Queue items to the existing queue.
queue.Enqueue(90);
queue.Enqueue(80);
queue.Enqueue(70);
queue.Enqueue(60);
// here we Dequeuing an queue item, it removes the first queue element
int firstElement = queue.Dequeue();
Console.WriteLine(“Dequeued queue element – ” + firstElement); // Result is – 90
// here we Peeking at the front element of queue without removing it
Console.WriteLine(“Front queue element- ” + queue.Peek()); // Result is – 80
// here it Checking if the queue contains a specific element or not
Console.WriteLine(“here it Contains queue element 70 – ” + queue.Contains(70)); // Result is – True
}
}
Special features of the C# Queue<T> Collection Data Type.
- Enqueue(T item) – This adds a new queue item to the end of the queue in the existing queue collection data type.
- Dequeue() – Removes the first element of a queue, item, from the current Queue collection data type, and returns and displays its value.
- Peek() – Returns the first item of a queue, without removing it.
- Contains(T item) – Checks the queue, item, and item in the current Queue collection data type to see if any items are present in the queue.
- Clear() – Removes all items from the queue, item, and item in the current Queue collection data type.
Dictionary<TKey, TValue> Collection Data Type in C#.
In C# programming, the Dictionary collection data type is a method for storing and processing values in a Dictionary data type in a key-value pair order. It allows C# users to store and immediately retrieve a value using a unique key value in the dictionary. This makes the Dictionary data type in C# programming useful for particular conditions or scenarios where the C# user needs to associate data with a specific key.
Create a Dictionary<TKey, TValue> Collection Data Type in C#.
using System;
using System.Collections.Generic;
class DictionaryExample
{
static void Main()
{
//Here we are creating a Dictionary data type with string keys and int values.
Dictionary<string, int> employees = new Dictionary<string, int>();
// Here we are adding key-value pairs to the dictionary in the database.
employees.Add(“Siddhi Deora”, 101);
employees.Add(“Bhavishi Deora”, 102);
employees.Add(“Harry Deora”, 103);
employees.Add(“Lalita”, 104);
// here we Accessing a value using a dictionary key
Console.WriteLine(“Siddhi Deora id – ” + employees[“Siddhi Deora”]); // Result is – 101
// here we Checking if a dictionary key exists
if (employees.ContainsKey(“Bhavishi Deora”))
{
Console.WriteLine(“Bhavishi Deora id – ” + employees[“Bhavishi Deora”]); // Result is – 102
}
// here it Removing a dictionary key-value pair
employees.Remove(“Lalita”);
// here it Iterating through the dictionary all element
foreach (var employee in employees)
{
Console.WriteLine(employee.Key + ” – ” + employee.Value);
}
}
}
Special features of the C# Dictionary<TKey, TValue> Collection Data Type.
- Add(TKey key, TValue value) – Adds a key-value pair to the existing dictionary data type dictionary.
- ContainsKey(TKey key) – Checks the existing dictionary data type dictionary to see if a key exists in the dictionary.
- ContainsValue(TValue value) – Checks the existing dictionary data type dictionary to see if a value exists in the dictionary.
- Remove(TKey key) – Removes a key-value pair corresponding to a key in the existing dictionary data type dictionary.
- TryGetValue(TKey key, out TValue value) – Tries to access the value for a given key in the existing dictionary data type dictionary.
- Clear() – Removes all key-value pairs from the dictionary in the existing dictionary data type dictionary.
Detail Comparison of Lists, Stacks, Queues, Dictionaries Collection Types
| Collection data Type | Access Pattern method | Where to Use Case |
| List<T> data type | List data type can be access or use Index-based, dynamic size order | You can use list data type, when you need a dynamically sized collection data, with random access to data elements. |
| Stack<T> data type | Stack data element can be access in LIFO (Last In, First Out) order | You can use stack data type, when you need to work with data in reverse sequence or order. (like, undo functionality or process). |
| Queue<T> data type | Queue data type element can be accessed in FIFO (First In, First Out) order | You can use queue data type, when you need to process data in the same order as it was received by user. (like, task scheduling etc). |
| Dictionary<TKey, TValue> | Dictionary data type element can be accessed Key-value pairs order | You can use dictionary data type, when you need to store key-value pairs for fast lookups by key value in stored location. (like, storing user info by ID). |
Lists, Stacks, Queues, Dictionaries: A summary of collection data types.
- List<T> collection data type – For use with dynamic arrays in C# programs, it’s the best choice when a C# user needs an ordered array collection with multiple additions and removals.
- Stack<T> collection data type – For C# programs, the LIFO operation (last-in, first-out) is the best choice for ordering data access, such as for undo operations or parsing methods.
- Queue<T> collection data type – For C# programs, the FIFO operation (first-in, first-out) is the best choice for data access, such as for processing tasks in the order they arrive.
- Dictionary<TKey, TValue> collection data type – For fast data element lookups and storing key-value pairs, the collection data type is the best choice for C# programs.
