Remove an item of a Dictionary with C#
To delete an element from dictionary in C# you directly useRemove()
method and pass the key as string parameter.Example
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
myDict.Remove("key1");
if(!myDict.ContainsKey("key1")){
Console.WriteLine("key1 does not Exists");
}
}
}
Output
key1 does not Exists