To remove a key-value pair from a dictionary in C#, you can use the `Remove` method of the `Dictionary
We can remove a key-value pair from a dictionary in C# using the `Remove` method. This example demonstrates how to remove a key-value pair by specifying the key and print the dictionary contents before and after the removal.
For example,
using System.Collections.Generic;
.myDictionary
with integer keys and string values.using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Declare and initialize a dictionary
Dictionary<int, string> myDictionary = new Dictionary<int, string>
{
{1, "One"},
{2, "Two"},
{3, "Three"}
};
// Print the dictionary contents before removal
Console.WriteLine("Dictionary contents before removal:");
foreach (KeyValuePair<int, string> kvp in myDictionary)
{
Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
// Key to remove
int keyToRemove = 2;
// Remove the key-value pair using the Remove method
myDictionary.Remove(keyToRemove);
// Print the dictionary contents after removal
Console.WriteLine("Dictionary contents after removal:");
foreach (KeyValuePair<int, string> kvp in myDictionary)
{
Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
}
}
Dictionary contents before removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Dictionary contents after removal: Key: 1, Value: One Key: 3, Value: Three
We can check if a key exists before attempting to remove a key-value pair in a dictionary in C# using the `Remove` method. This example demonstrates how to check for key existence and remove the key-value pair if it exists, then print the dictionary contents before and after the removal.
For example,
using System.Collections.Generic;
.myDictionary
with integer keys and string values.using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Declare and initialize a dictionary
Dictionary<int, string> myDictionary = new Dictionary<int, string>
{
{1, "One"},
{2, "Two"},
{3, "Three"}
};
// Print the dictionary contents before removal
Console.WriteLine("Dictionary contents before removal:");
foreach (KeyValuePair<int, string> kvp in myDictionary)
{
Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
// Key to remove
int keyToRemove = 4;
// Check if the key exists and remove it if it does
if (myDictionary.ContainsKey(keyToRemove))
{
myDictionary.Remove(keyToRemove);
Console.WriteLine("Key {0} was removed from the dictionary.", keyToRemove);
}
else
{
Console.WriteLine("Key {0} does not exist in the dictionary.", keyToRemove);
}
// Print the dictionary contents after removal
Console.WriteLine("Dictionary contents after removal:");
foreach (KeyValuePair<int, string> kvp in myDictionary)
{
Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
}
}
Dictionary contents before removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three Key 4 does not exist in the dictionary. Dictionary contents after removal: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three
In this tutorial, we learned How to Remove a Key-Value Pair from a Dictionary in C# language with well detailed examples.