![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dictionary.Remove() Method in C#
The Dictionary.Remove() property in C# is used to remove the value with the specified key from the Dictionary<TKey, TValue>.
Syntax
Following is the syntax −
public bool Remove (TKey key);
Above, the key is the key to remove.
Example
Let us now see an example to implement the Dictionary.Remove() property −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("One", "Kagido"); dict.Add("Two", "Ngidi"); dict.Add("Three", "Devillers"); dict.Add("Four", "Smith"); dict.Add("Five", "Warner"); Console.WriteLine("Count of elements = "+dict.Count); Console.WriteLine("Removing some keys..."); dict.Remove("Four"); dict.Remove("Five"); Console.WriteLine("Count of elements (updated) = "+dict.Count); Console.WriteLine("
Key/value pairs..."); foreach(KeyValuePair<string, string> res in dict){ Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); } Console.Write("
All the keys..
"); Dictionary<string, string>.KeyCollection allKeys = dict.Keys; foreach(string str in allKeys){ Console.WriteLine("Key = {0}", str); } } }
Output
This will produce the following output −
Count of elements = 5 Removing some keys... Count of elements (updated) = 3 Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers All the keys.. Key = One Key = Two Key = Three
Example
Let us now see another example to implement the Dictionary.Remove() method −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("One", "Kagido"); dict.Add("Two", "Ngidi"); dict.Add("Three", "Devillers"); dict.Add("Four", "Smith"); dict.Add("Five", "Warner"); Console.WriteLine("Count of elements = "+dict.Count); Console.Write("
All the keys..
"); Dictionary<string, string>.KeyCollection allKeys = dict.Keys; foreach(string str in allKeys){ Console.WriteLine("Key = {0}", str); } Console.WriteLine("Removing some keys..."); dict.Remove("Four"); dict.Remove("Five"); Console.WriteLine("Count of elements (updated) = "+dict.Count); Console.Write("
All the keys..updated
"); foreach(string str in allKeys){ Console.WriteLine("Key = {0}", str); } Console.WriteLine("
Key/value pairs..."); foreach(KeyValuePair<string, string> res in dict){ Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); } } }
Output
This will produce the following output −
Count of elements = 5 All the keys.. Key = One Key = Two Key = Three Key = Four Key = Five Removing some keys... Count of elements (updated) = 3 All the keys..updated Key = One Key = Two Key = Three Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers
Advertisements