C# Dictionary Methods

Beginner
5 min read
Updated: Nov 2026
10 method guides
Method index

About This Index

This page is the C# Dictionary Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. All examples use fixed data such as Dictionary<string, int> with names and ages, so the output you see is the output you get.

Learn what a Dictionary is for on /c-sharp/dictionary. Use this page when you need a specific API such as Add(), TryGetValue(), or ContainsKey().

Keys are unique.

Every key can appear only once. Add throws if you repeat a key, TryAdd returns false, and the indexer d[key] = value overwrites. Also do not depend on the order of pairs when you loop; a Dictionary does not promise one.

How to Call a Dictionary Method

Every Dictionary method is an instance method, so you call it on your dictionary variable:

C#
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);                              // add a pair
bool added = ages.TryAdd("Ravi", 31);              // add if the key is new
bool has = ages.ContainsKey("Asha");               // look up a key
bool found = ages.TryGetValue("Ravi", out int age); // read safely
bool gone = ages.Remove("Asha");                   // remove a pair

C# Dictionary Methods Table

All method guides in this C# tutorial series (10 tutorials), grouped by purpose.

Add / update

2 methods

Put new key and value pairs into a dictionary. Add throws on a duplicate key, TryAdd returns false instead, and the indexer overwrites.

MethodDescriptionTutorial
Add()Add a new key and value to a Dictionary; it throws if the key already exists.Try it
TryAdd()Add a key and value only if the key is new; returns false instead of throwing.Try it

Look up

3 methods

Ask whether a key or a value exists, and read a value safely without an exception.

MethodDescriptionTutorial
ContainsKey()Check if a key exists in the Dictionary (true or false).Try it
ContainsValue()Check if any entry has a given value (true or false), by scanning all entries.Try it
TryGetValue()Read a value by key safely, with one lookup and no exception if the key is missing.Try it

Remove

2 methods

Delete one pair by key or empty the whole dictionary at once.

MethodDescriptionTutorial
Remove()Remove a key and its value; returns true if it was found, false if not.Try it
Clear()Remove all keys and values from the Dictionary (Count becomes 0).Try it

Capacity

2 methods

Reserve space before a big load, or shrink unused space after a big clean-up.

MethodDescriptionTutorial
EnsureCapacity()Pre-size the Dictionary so it can hold at least a number of entries without growing.Try it
TrimExcess()Shrink the Dictionary's reserved space to fit its current number of entries.Try it

Iteration

1 method

Walk through every key and value pair with foreach or a manual enumerator.

MethodDescriptionTutorial
GetEnumerator()Get an enumerator that visits every KeyValuePair; foreach calls it for you.Try it

Frequently Asked Questions

They are the methods on System.Collections.Generic.Dictionary<TKey,TValue> that add pairs, look up keys and values, remove pairs, manage capacity, and loop through every key and value.
This index lists 10 guides: Add, TryAdd, ContainsKey, ContainsValue, TryGetValue, Remove, Clear, EnsureCapacity, TrimExcess, and GetEnumerator.
Use Add when a duplicate key is a bug and should throw. Use TryAdd when the first value should win. Use the indexer, such as ages["Asha"] = 26, when the newest value should win.
Use TryGetValue(key, out value). It returns true or false and does only one lookup. The indexer throws KeyNotFoundException for a missing key.
Read the C# Dictionary hub first. Then open Add(), TryGetValue(), ContainsKey(), and Remove() from the tables below.

Start with Add()

Put your first key and value pair into a dictionary. It is the call every beginner needs first.

Add() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful