PositiveGuy
PositiveGuy

Reputation: 47743

Remove a Key from Dictionary by key name

I'm trying to remove a key from my dictionary if the key is a certain key.

parameterList is a dictionary<string,string>

parameterList.Remove(parameterList.Where(k => String.Compare(k.Key, "someKeyName") == 0)); 

Upvotes: 41

Views: 83028

Answers (2)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Simply remove by key:

parameterList.Remove("someKeyName");

To check:

if (parameterList.Remove("someKeyName"))
{
    // key removed
}
else
{
    // dictionary doesn't contain the key
}

Upvotes: 35

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

This should be enough:

parameterList.Remove(key);

Upvotes: 75

Related Questions