
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
HybridDictionary Class in C#
The HybridDictionary class implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.
Following are the properties of the HybridDictionary class −
Sr.No | Property & Description |
---|---|
1 |
Count Gets the number of key/value pairs contained in the HybridDictionary. |
2 |
IsFixedSize Gets a value indicating whether the HybridDictionary has a fixed size. |
3 |
IsReadOnly Gets a value indicating whether the HybridDictionary is read-only. |
4 |
IsSynchronized Gets a value indicating whether the HybridDictionary is synchronized (thread safe). |
5 |
Item[Object] Gets or sets the value associated with the specified key. |
6 |
Keys Gets an ICollection containing the keys in the HybridDictionary. |
7 |
SyncRoot Gets an object that can be used to synchronize access to the HybridDictionary. |
8 |
Values Gets an ICollection containing the values in the HybridDictionary. |
Following are some of the methods of the HybridDictionary class −
Sr.No | Methods & Description |
---|---|
1 |
Add(Object, Object) Adds an entry with the specified key and value into the HybridDictionary. |
2 |
Clear() Removes all entries from the HybridDictionary. |
3 |
Contains(Object) Determines whether the HybridDictionary contains a specific key.p> |
4 |
CopyTo(Array, Int32) Copies the HybridDictionary entries to a onedimensional Array instance at the specified index. |
5 |
Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object) |
6 |
GetEnumerator() Returns an IDictionaryEnumerator that iterates through the HybridDictionary. |
7 |
GetHashCode() Serves as the default hash function. (Inherited from Object) |
8 |
GetType() Gets the Type of the current instance. (Inherited from Object) |
To count the number of key/value pairs in HybridDictionary, the code is as follows −
Example
Let us now see some examples −
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { HybridDictionary dict1 = new HybridDictionary(); dict1.Add("A", "SUV"); dict1.Add("B", "MUV"); dict1.Add("C", "AUV"); Console.WriteLine("HybridDictionary1 elements..."); foreach(DictionaryEntry d in dict1) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count); HybridDictionary dict2 = new HybridDictionary(); dict2.Add("1", "One"); dict2.Add("2", "Two"); dict2.Add("3", "Three"); dict2.Add("4", "Four"); dict2.Add("5", "Five"); dict2.Add("6", "Six"); Console.WriteLine("
HybridDictionary2 elements..."); foreach(DictionaryEntry d in dict2) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict1.Count); dict2.Clear(); Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count); } }
Output
This will produce the following output −
HybridDictionary1 elements... A SUV B MUV C AUV Count of Key/value pairs in Dictionary1 = 3 HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Count of Key/value pairs in Dictionary2 = 3 Count of Key/value pairs in Dictionary2 (Updated) = 0
To check if HybridDictionary is synchronized, the code is as follows −
Example
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { HybridDictionary dict1 = new HybridDictionary(); dict1.Add("A", "Books"); dict1.Add("B", "Electronics"); dict1.Add("C", "Smart Wearables"); dict1.Add("D", "Pet Supplies"); dict1.Add("E", "Clothing"); dict1.Add("F", "Footwear"); Console.WriteLine("HybridDictionary1 elements..."); foreach(DictionaryEntry d in dict1) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Is the HybridDictionary1 having fixed size? = "+dict1.IsFixedSize); Console.WriteLine("If HybridDictionary1 read-only? = "+dict1.IsReadOnly); Console.WriteLine("Is HybridDictionary1 synchronized = "+dict1.IsSynchronized); HybridDictionary dict2 = new HybridDictionary(); dict2.Add("1", "One"); dict2.Add("2", "Two"); dict2.Add("3", "Three"); dict2.Add("4", "Four"); dict2.Add("5", "Five"); dict2.Add("6", "Six"); Console.WriteLine("
HybridDictionary2 elements..."); foreach(DictionaryEntry d in dict2) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = "+(dict1.Equals(dict2))); Console.WriteLine("Is the HybridDictionary2 having fixed size? = "+dict2.IsFixedSize); Console.WriteLine("If HybridDictionary2 read-only? = "+dict2.IsReadOnly); Console.WriteLine("Is HybridDictionary2 synchronized = "+dict2.IsSynchronized); } }
Output
This will produce the following output −
HybridDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the HybridDictionary1 having fixed size? = False If HybridDictionary1 read-only? = False Is HybridDictionary1 synchronized = False HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is HybridDictionary1 equal to HybridDictionary2? = False Is the HybridDictionary2 having fixed size? = False If HybridDictionary2 read-only? = False Is HybridDictionary2 synchronized = False
Advertisements