![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
Clear a Hashtable in C#
Clear a Hashtable, using the Clear() method in C#.
The following is our Hashtable −
Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul");
Use the clear method.
h.Clear();
If you will now try to display the Hashtable, nothing would get display since the Hashtable is empty.
Example
using System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul"); Console.WriteLine("Keys and Values list:"); foreach (var key in h.Keys ) { Console.WriteLine("Key = {0}, Value = {1}",key , h[key]); } // clear all key value pairs Console.WriteLine("The Hashtable key and Value Pairs are empty now..."); h.Clear(); foreach (var key in h.Keys ) { Console.WriteLine("Key = {0}, Value = {1}",key , h[key]); } } }
Output
Keys and Values list: Key = 3, Value = Rahul Key = 2, Value = Sachin Key = 1, Value = Amit The Hashtable key and Value Pairs are empty now...
Advertisements