Remove ExamplesΒΆ

Removes existing key/value pair from a dictionary using the key as the reference.

dictionaryName.Remove(key);

Example

Dictionary<string, int> moons = new Dictionary<string, int>
{
   {"Mercury", 0},
   {"Venus", 0},
   {"Earth", 1},
   {"Mars", 2},
   {"Jupiter", 79},
   {"Saturn", 82},
   {"Uranus", 27},
   {"Neptune", 14},
   {"Pluto", 5}
};

Console.WriteLine(moons.ContainsValue(3));
     Console.WriteLine(moons.ContainsValue(82));

Output

[Mercury, 0]
[Venus, 0]
[Earth, 1]
[Mars, 2]
[Jupiter, 79]
[Saturn, 82]
[Uranus, 27]
[Neptune, 14]
[Pluto, 5]
//oops forgot Pluto's not considered a planet

moons.Remove("Pluto");

foreach(KeyValuePair<string,int> moon in moons)
{
   Console.WriteLine(moon);
}

Output

[Mercury, 0]
[Venus, 0]
[Earth, 1]
[Mars, 2]
[Jupiter, 79]
[Saturn, 82]
[Uranus, 27]
[Neptune, 14]