- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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...
- Related Articles
- Check if a Hashtable is equal to another Hashtable in C#
- Clear a StringBuilder in C#
- Clear a list in C#
- Hashtable vs. Dictionary in C#
- Remove an item from a Hashtable in C#
- C# Program to find a key in a Hashtable
- C# Program to find a value in a Hashtable
- Clear a Linked List in C#
- Check if Hashtable has a fixed size in C#
- Creating a synchronized wrapper for the Hashtable in C#
- Why is a Dictionary preferred over a Hashtable in C#?
- What is the Hashtable class in C#?
- Working with Hashtable and Dictionary in C#
- Difference between Dictionary and Hashtable in C#
- Difference between HashTable and Dictionary in C#

Advertisements