TL ; DR:
A Dictionary's Remove method takes a key. It deletes that key and its value.
Dictionary<string, int> toys = new Dictionary<string, int>();
toys["ball"] = 3;
toys["kite"] = 1;
toys.Remove("kite");
Console.WriteLine(toys.Count); // 1
Full lesson:
Sometimes an entry has to go. A shop stops selling an item. A player leaves the game.
Call Remove on the dictionary with the key. The key and its value go away together.
Count drops by 1. The key is gone, so toys.ContainsKey("kite") is now false.
Remove returns true when it deleted the key. For a missing key it returns false, and nothing changes.
Watch out: reading toys["kite"] after the delete throws a KeyNotFoundException. Check with ContainsKey first.
Your task
A pet shop counts what it has for sale in animals. Each key is a kind of animal. Its value is how many the shop has.
The last rabbit just went home with a family. Delete the key "rabbit" from animals.
Then print animals.Count again. The code at the bottom checks if any rabbits are left.
Expected output:
3
2
No rabbits leftFree plan: 30 tutor messages. PRO is unlimited.
