C# Programming Language: Dictionaries

Creating Dictionary

using System.Collections.Generic;
Dictionary<int, string> myDict = new Dictionary<int, string>();

Adding Elements Into The Dictionary

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
foreach(var element in myDict)
{
Console.WriteLine(element.Key + ": " + element.Value);
}
Output:
1: One
4: Four
17: Seventeen

Retrieving The Keys of a Dictionary

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
List<int> myKeys = new List<int>(myDict.Keys);
Console.WriteLine(String.Join(" ", myKeys));
Output:
1 4 17

Retrieving The Values of a Dictionary

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
List<string> myValues = new List<string>(myDict.Values);
Console.WriteLine(String.Join(" ", myValues));
Output:
One Four Seventeen

Checking The Size of The Dictionary

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
Console.WriteLine(myDict.Count);
Output:
3

Checking if The Dictionary Contains a Specific Key

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
Console.WriteLine(myDict.ContainsKey(1));
Console.WriteLine(myDict.ContainsKey(2));
Output:
True
False

Checking if The Dictionary Contains a Specific Value

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
Console.WriteLine(myDict.ContainsValue("One"));
Console.WriteLine(myDict.ContainsValue("Two"));
Output:
True
False

Removing an Element From a Dictionary

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
myDict.Remove(1);
foreach(var element in myDict)
{
Console.WriteLine(element.Key + ": " + element.Value);
}
Output:
4: Four
17: Seventeen

Clearing out a Dictionary

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
myDict.Clear();
foreach(var element in myDict)
{
Console.WriteLine(element.Key + ": " + element.Value);
}
Output:

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to check version of your spring application on production

adb is not recognized as an internal or external command, operable program or batch file

🏎 ZODIACS | Play 2 Earn $ZDC 🏎

NoSQL Databases

Editor’s Pick of the Week (May 1–7, 2021)

Increase The Efficiency Of Your Operational Processes With An Enterprise App Store

Binary Trees

The best of two worlds: working with VSCode and Windows Subsystem for Linux (Python)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesse L

Jesse L

More from Medium

C# Programming Language: Queue

Async programming vs Parallel programming (Basic)

Deferred execution vs Immediate execution — LINQ

Scoped, Transient and Singleton