C# Dictionary
Create a Dictionary<string, int> named bin with new Dictionary<string, int>(). Add the pair "oak" to 3, then add "pine" to 8. Write bin["oak"] and then write bin.Count. Keep the work in Pine with Main. Keep using System.Collections.Generic; under using System;. Run with dotnet run. The screen should show two lines: 3 then 2. A dictionary stores a key that points to a value. The indexer reads by key, not by a list slot. Count is how many pairs are in the map. Keep the starting public class named Pine.
A key that finds a value
On the list-methods page you asked a bag with Contains, dropped a value with Remove, and found a slot with IndexOf. A Dictionary is a different shape. You store pairs. You look up a value by the key you chose, not by bag[0]. The name bin is the map you keep on this page. The keys are short words. The values are int numbers so the screen stays easy to read.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any leftover List<int> bag demo that only calls Remove and prints bag[0] and bag.Count. Delete any Box b = new Box(4); leftover. Delete any ReadLine line. The file you keep has no wait for typing. A clean Main makes the two-line dictionary screen easy to trust.
At the top keep using System;. Keep using System.Collections.Generic;. Inside Main, write Dictionary<string, int> bin = new Dictionary<string, int>();. Then bin.Add("oak", 3);. Then write Console.WriteLine(bin["oak"]); for a first check. Save before you run that short shape, then extend to two Adds and Count as the kept demo. The one-Add run is only a checkpoint; the page's kept answer is still 3 then 2.
Dictionary needs two type arguments: the key kind and the value kind. On this page that is <string, int>. Do not write a plain Dictionary bin without the type arguments in the teaching file. The angle brackets are part of the type name you type in the editor.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
Dictionary<string, int> bin = new Dictionary<string, int>();
bin.Add("oak", 3);
Console.WriteLine(bin["oak"]);
}
}
Pine adds two pairs and writes Count
Inside Main, after the empty map, write bin.Add("oak", 3); then bin.Add("pine", 8);. Write bin["oak"] first so the screen shows the value for that key. Then write bin.Count so the screen shows how many pairs you stored. You are not calling bag.Remove(3); on this page. That was the list-methods lesson.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
Dictionary<string, int> bin = new Dictionary<string, int>();
bin.Add("oak", 3);
bin.Add("pine", 8);
Console.WriteLine(bin["oak"]);
Console.WriteLine(bin.Count);
}
}
Read the kept file from the top. using System; prepares console names. using System.Collections.Generic; prepares Dictionary. public class Pine is the starter. public static void Main() is where the runtime starts. new Dictionary<string, int>() builds an empty map. The first Add stores key "oak" with value 3. The second Add stores key "pine" with value 8. Writing bin["oak"] prints 3. Writing bin.Count prints 2.
Run it with dotnet run. The program writes two lines and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the dictionary work. The screen shows 3 then 2. If the screen shows 8 then 1, a leftover list-methods Remove demo may still be running. If the build cannot find Dictionary, restore using System.Collections.Generic;.
If you write bin[0] hoping for a list slot, the build fails or the meaning is wrong — dictionary lookup wants the key type, here a string. If you call bin["oak"] before any Add for that key, the run can throw because the key is missing. Add the pair first. If you Add the same key twice, the second Add throws — this page keeps two different keys.
ContainsKey as a quiet check
You can ask whether a key is present before you read it. After the two Adds, bin.ContainsKey("pine") is true. A short file prints that check so you see the yes answer on the screen.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
Dictionary<string, int> bin = new Dictionary<string, int>();
bin.Add("oak", 3);
bin.Add("pine", 8);
Console.WriteLine(bin.ContainsKey("pine"));
}
}
Run the ContainsKey file with dotnet run. The screen shows True. That run is a second checkpoint, not the kept two-line answer. When you want 3 then 2 on screen, use the two-Add indexer file. Do not treat ContainsKey as Count. Count is still the number of pairs. ContainsKey only answers whether one key is present.
Writing bin["pine"] after the two Adds prints 8. That is valid, but the kept demo prints bin["oak"] first so the quiz answer stays 3 then 2. If the screen shows only True, you are on the ContainsKey file — switch back for the main checkpoint.
Compared with List and with a fixed array habit
A list holds values in order under indexes. A dictionary holds pairs under keys you choose. A leftover bag.Remove(3); Console.WriteLine(bag[0]); is a different lesson. For this page keep Dictionary<string, int> bin and the Add calls with string keys.
You can still use lists when order by slot matters. This page does not replace every list. It shows the keyed map shape you will leave behind when the hashset page teaches a set of unique values without paired ints. Stay with string keys and int values here so the indexer and Count numbers stay easy to read on the screen.
What this leaves for hashset
This page builds a dictionary, adds pairs, reads by key, and reads Count. A later page — hashset — stores unique values without a paired second type on each entry. Stay with create, Add, indexer, and Count here. Do not invent every dictionary method on this page; the lesson goal is the keyed bin itself. When dotnet run prints 3 then 2 from the two-Add file, you have the checkpoint this page asks for.
Do not nest a second Main. Do not leave List Remove demos in the file. If the build cannot find Dictionary, check using System.Collections.Generic;. If the screen shows only one line, you may have forgotten the second WriteLine — keep both bin["oak"] and bin.Count on the two-Add demo. If the screen shows True alone, you are on the ContainsKey file; that is fine for the second check, then return to the two-Add shape for the quiz answer on screen.
You can write Dictionary<string, int> bin = new Dictionary<string, int>();, call bin.Add("oak", 3); and bin.Add("pine", 8);, then write bin["oak"] and bin.Count. Run with dotnet run. The screen shows 3 then 2. The map held two pairs. The key found the value. Count reported the length. Think of Dictionary as a labeled bin: you store a number under a word, then ask for that word again. Pine stays the door you open with dotnet run. The next page is hashset, where storage shifts from keyed pairs to a set of unique values.
Keep using System.Collections.Generic;. Keep <string, int> on the dictionary type. Keep leftover list Remove demos out of Main so the screen cannot show 8 then 1 by mistake. Those habits are the checkpoint before you move on.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top