codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

33 / 60 topics
33Collections in C#34List in C#35Dictionary in C#36Queue in C#37Stack in C#
Tutorials/C# Programming/Collections in C#
🔷C# Programming

Collections in C#

Updated 2026-05-15
10 min read

Collections in C#

Introduction

In the world of programming, managing data efficiently is crucial. C# provides a rich set of built-in collections that help developers organize and manipulate data effectively. In this tutorial, we will explore three fundamental generic collections in C#: List, Dictionary, and Queue. These collections are essential for handling various data structures and operations.

Concept

List

A List is an ordered collection of elements that can be accessed by index. It allows dynamic resizing as elements are added or removed. Lists are ideal when you need a flexible array-like structure with additional functionalities like sorting, searching, and more.

Dictionary

A Dictionary is a collection of key-value pairs where each key must be unique. Dictionaries provide fast access to data based on keys, making them highly efficient for scenarios requiring quick lookups.

Queue

A Queue follows the First-In-First-Out (FIFO) principle, meaning that the first element added to the queue is the first one to be removed. Queues are useful in scenarios where tasks need to be processed in the order they arrive, such as task scheduling or message queues.

Examples

List Example

Let's start by creating a List of integers and perform some basic operations:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 // Create a new List of integers
9 List<int> numbers = new List<int>();
10
11 // Add elements to the list
12 numbers.Add(10);
13 numbers.Add(20);
14 numbers.Add(30);
15
16 // Access elements by index
17 Console.WriteLine("First element: " + numbers[0]);
18
19 // Iterate over the list
20 foreach (int number in numbers)
21 {
22 Console.WriteLine(number);
23 }
24
25 // Remove an element
26 numbers.Remove(20);
27
28 // Check if an element exists
29 bool contains = numbers.Contains(30);
30 Console.WriteLine("Contains 30: " + contains);
31 }
32}
Output
First element: 10
10
20
30
Contains 30: True

Dictionary Example

Next, let's create a Dictionary to store student names and their corresponding grades:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 // Create a new Dictionary with string keys and int values
9 Dictionary<string, int> grades = new Dictionary<string, int>();
10
11 // Add key-value pairs to the dictionary
12 grades.Add("Alice", 95);
13 grades.Add("Bob", 88);
14 grades.Add("Charlie", 76);
15
16 // Access a value by key
17 Console.WriteLine("Grade of Alice: " + grades["Alice"]);
18
19 // Iterate over the dictionary
20 foreach (KeyValuePair<string, int> entry in grades)
21 {
22 Console.WriteLine(entry.Key + ": " + entry.Value);
23 }
24
25 // Check if a key exists
26 bool hasBob = grades.ContainsKey("Bob");
27 Console.WriteLine("Has Bob: " + hasBob);
28
29 // Remove a key-value pair
30 grades.Remove("Charlie");
31
32 // Get the number of elements in the dictionary
33 int count = grades.Count;
34 Console.WriteLine("Number of students: " + count);
35 }
36}
Output
Grade of Alice: 95
Alice: 95
Bob: 88
Charlie: 76
Has Bob: True
Number of students: 2

Queue Example

Finally, let's create a Queue to simulate a simple task processing system:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 // Create a new Queue of strings
9 Queue<string> tasks = new Queue<string>();
10
11 // Enqueue tasks
12 tasks.Enqueue("Task 1");
13 tasks.Enqueue("Task 2");
14 tasks.Enqueue("Task 3");
15
16 // Dequeue tasks and process them
17 while (tasks.Count > 0)
18 {
19 string task = tasks.Dequeue();
20 Console.WriteLine("Processing: " + task);
21 }
22
23 // Check if the queue is empty
24 bool isEmpty = tasks.Count == 0;
25 Console.WriteLine("Queue is empty: " + isEmpty);
26 }
27}
Output
Processing: Task 1
Processing: Task 2
Processing: Task 3
Queue is empty: True

What's Next?

In the next section, we will dive deeper into the List collection in C#, exploring its methods and properties in more detail. Understanding how to effectively use lists will be crucial for many programming tasks.

Stay tuned!


PreviousSerialization in C#Next List in C#

Recommended Gear

Serialization in C#List in C#