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

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

Dictionary in C#

Updated 2026-05-15
10 min read

Dictionary in C#

Introduction

In the world of programming, data structures are fundamental building blocks that help organize and manage data efficiently. One such powerful data structure is the Dictionary, which allows you to store key-value pairs. This tutorial will guide you through understanding how to use dictionaries in C#, from basic concepts to practical applications.

Concept

A dictionary in C# is a collection of key-value pairs where each key is unique. Dictionaries are part of the System.Collections.Generic namespace and provide fast access to elements based on their keys. This makes them highly efficient for scenarios where you need quick lookups, insertions, and deletions.

Key Features

  • Key Uniqueness: Each key in a dictionary must be unique.
  • Fast Access: Provides average O(1) time complexity for operations like add, remove, and find.
  • Dynamic Size: Can grow or shrink as needed during runtime.

Examples

Let's dive into some practical examples to understand how dictionaries work in C#.

Example 1: Creating a Dictionary

First, let's create a simple dictionary that maps integers to strings.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 // Create a new dictionary with integer keys and string values
9 Dictionary<int, string> numberNames = new Dictionary<int, string>();
10
11 // Add key-value pairs to the dictionary
12 numberNames.Add(1, "One");
13 numberNames.Add(2, "Two");
14 numberNames.Add(3, "Three");
15
16 // Access a value using its key
17 Console.WriteLine(numberNames[1]); // Output: One
18
19 // Check if a key exists in the dictionary
20 if (numberNames.ContainsKey(4))
21 {
22 Console.WriteLine("Four is in the dictionary.");
23 }
24 else
25 {
26 Console.WriteLine("Four is not in the dictionary.");
27 }
28 }
29}
Output
One
Four is not in the dictionary.

Example 2: Iterating Over a Dictionary

You can iterate over a dictionary using various methods. Here, we'll use a foreach loop to print all key-value pairs.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 Dictionary<int, string> numberNames = new Dictionary<int, string>
9 {
10 { 1, "One" },
11 { 2, "Two" },
12 { 3, "Three" }
13 };
14
15 // Iterate over the dictionary using foreach
16 foreach (KeyValuePair<int, string> kvp in numberNames)
17 {
18 Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
19 }
20 }
21}
Output
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three

Example 3: Removing Elements

You can remove elements from a dictionary using the Remove method. Here's how you can do it:

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 Dictionary<int, string> numberNames = new Dictionary<int, string>
9 {
10 { 1, "One" },
11 { 2, "Two" },
12 { 3, "Three" }
13 };
14
15 // Remove a key-value pair by key
16 if (numberNames.Remove(2))
17 {
18 Console.WriteLine("Key 2 removed successfully.");
19 }
20
21 // Iterate over the dictionary to verify removal
22 foreach (KeyValuePair<int, string> kvp in numberNames)
23 {
24 Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
25 }
26 }
27}
Output
Key 2 removed successfully.
Key: 1, Value: One
Key: 3, Value: Three

What's Next?

Now that you have a good understanding of dictionaries in C#, the next topic to explore is the Queue. Queues are another essential data structure used for managing collections of items where elements are added at one end and removed from the other. Stay tuned for more tutorials on codingstuff.io!

Info

Remember, practice makes perfect! Try creating your own dictionaries with different types of keys and values to get a better grasp of how they work.


PreviousList in C#Next Queue in C#

Recommended Gear

List in C#Queue in C#