{"id":200,"date":"2014-08-20T13:10:48","date_gmt":"2014-08-20T13:10:48","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=200"},"modified":"2014-08-20T13:10:48","modified_gmt":"2014-08-20T13:10:48","slug":"dictionary-and-hashtable","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=200","title":{"rendered":"Dictionary  ,Hashtable and HashSet"},"content":{"rendered":"<p><b>HashTable<\/b><\/p>\n<p>Represents a collection of key\/value pairs that are organized based on the hash code of the key.<\/p>\n<p>Hashtable optimizes lookups. It computes an adding hash of each key. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type.<\/p>\n<p><b>Dictionary<\/b><\/p>\n<p>A dictionary is used where fast lookups are critical. The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings. Dictionary requires a special syntax form.<\/p>\n<p>Dictionary is used when we have many different elements. We specify its key type and its value type. It provides good performance.<\/p>\n<p><b>Differences between Hashtable and\u00a0Dictionary<\/b><\/p>\n<p><b>Dictionary:<\/b><\/p>\n<ul>\n<li>It returns error if we try to find a key which does not exist.<\/li>\n<li>It is faster than a Hashtable because there is no boxing and unboxing.<\/li>\n<li>Only public static members are thread safe.<\/li>\n<li>Dictionary is a generic type which means we can use it with any\u00a0data type.<\/li>\n<\/ul>\n<p><b>Hashtable:<\/b><\/p>\n<ul>\n<li>It returns null if we try to find a key which does not exist.<\/li>\n<li>It is slower than dictionary because it requires boxing and unboxing.<\/li>\n<li>All the members in a Hashtable are thread safe,<\/li>\n<li>Hashtable is not a generic type,<\/li>\n<\/ul>\n<p><b>Hashtable and Dictionary Collection Types<\/b><b><\/b><\/p>\n<ul>\n<li>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">System.Collections.Hashtable<\/a> class, and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/xfhwa508(v=vs.110).aspx\">System.Collections.Generic.Dictionary&lt;TKey, TValue&gt;<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd287191(v=vs.110).aspx\">System.Collections.Concurrent.ConcurrentDictionary&lt;TKey, TValue&gt;<\/a> generic classes, implement the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.idictionary(v=vs.110).aspx\">System.Collections.IDictionary<\/a> interface. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/xfhwa508(v=vs.110).aspx\">Dictionary&lt;TKey, TValue&gt;<\/a> generic class also implements the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/s4ys34ea(v=vs.110).aspx\">IDictionary&lt;TKey, TValue&gt;<\/a> generic interface. Therefore, each element in these collections is a key-and-value pair.<\/li>\n<li>A <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> object consists of buckets that contain the elements of the collection. A bucket is a virtual subgroup of elements within the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a>, which makes searching and retrieving easier and faster than in most collections. Each bucket is associated with a hash code, which is generated using a hash function and is based on the key of the element.<\/li>\n<li>The generic <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb359438(v=vs.110).aspx\">HashSet&lt;T&gt;<\/a> class is an unordered collection for containing unique elements. For more information about this collection, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb397727(v=vs.110).aspx\">HashSet Collection Type<\/a>.<\/li>\n<li>A hash function is an algorithm that returns a numeric hash code based on a key. The key is the value of some property of the object being stored. A hash function must always return the same hash code for the same key. It is possible for a hash function to generate the same hash code for two different keys, but a hash function that generates a unique hash code for each unique key results in better performance when retrieving elements from the hash table.<\/li>\n<li>Each object that is used as an element in a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> must be able to generate a hash code for itself by using an implementation of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.gethashcode(v=vs.110).aspx\">GetHashCode<\/a> method. However, you can also specify a hash function for all elements in a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> by using a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> constructor that accepts an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ihashcodeprovider(v=vs.110).aspx\">IHashCodeProvider<\/a> implementation as one of its parameters.<\/li>\n<li>When an object is added to a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a>, it is stored in the bucket that is associated with the hash code that matches the object&#8217;s hash code. When a value is being searched for in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a>, the hash code is generated for that value, and the bucket associated with that hash code is searched.<\/li>\n<li>For example, a hash function for a string might take the ASCII codes of each character in the string and add them together to generate a hash code. The string &#8220;picnic&#8221; would have a hash code that is different from the hash code for the string &#8220;basket&#8221;; therefore, the strings &#8220;picnic&#8221; and &#8220;basket&#8221; would be in different buckets. In contrast, &#8220;stressed&#8221; and &#8220;desserts&#8221; would have the same hash code and would be in the same bucket.<\/li>\n<li>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/xfhwa508(v=vs.110).aspx\">Dictionary&lt;TKey, TValue&gt;<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd287191(v=vs.110).aspx\">ConcurrentDictionary&lt;TKey, TValue&gt;<\/a>classes have the same functionality as the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> class. A <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/xfhwa508(v=vs.110).aspx\">Dictionary&lt;TKey, TValue&gt;<\/a> of a specific type (other than <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object(v=vs.110).aspx\">Object<\/a>) provides better performance than a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> for value types. This is because the elements of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\">Hashtable<\/a> are of type <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object(v=vs.110).aspx\">Object<\/a>; therefore, boxing and unboxing typically occur when you store or retrieve a value type. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd287191(v=vs.110).aspx\">ConcurrentDictionary&lt;TKey, TValue&gt;<\/a>class should be used when multiple threads might be accessing the collection simultaneously.<\/li>\n<\/ul>\n<p><b>Dictionary<\/b><\/p>\n<p><b>Hashtable<\/b><\/p>\n<p>Each element is a key\/value pair stored in a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.dictionaryentry(v=vs.110).aspx\">DictionaryEntry<\/a> object. A key cannot be null, but a value can be.<\/p>\n<p>The objects used as keys by a Hashtable are required to override the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.gethashcode(v=vs.110).aspx\">Object.GetHashCode<\/a> method (or the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ihashcodeprovider(v=vs.110).aspx\">IHashCodeProvider<\/a> interface) and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.equals(v=vs.110).aspx\">Object.Equals<\/a> method (or the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.icomparer(v=vs.110).aspx\">IComparer<\/a> interface). The implementation of both methods and interfaces must handle case sensitivity the same way; otherwise, the Hashtable might behave incorrectly. For example, when creating a Hashtable, we \u00a0must use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.caseinsensitivehashcodeprovider(v=vs.110).aspx\">CaseInsensitiveHashCodeProvider<\/a> class (or any case-insensitive <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ihashcodeprovider(v=vs.110).aspx\">IHashCodeProvider<\/a> implementation) with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.caseinsensitivecomparer(v=vs.110).aspx\">CaseInsensitiveComparer<\/a> class (or any case-insensitive <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.icomparer(v=vs.110).aspx\">IComparer<\/a> implementation).<\/p>\n<p>Furthermore, these methods must produce the same results when called with the same parameters while the key exists in the Hashtable. An alternative is to use a Hashtable constructor with an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.iequalitycomparer(v=vs.110).aspx\">IEqualityComparer<\/a> parameter. If key equality were simply reference equality, the inherited implementation of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.gethashcode(v=vs.110).aspx\">Object.GetHashCode<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.equals(v=vs.110).aspx\">Object.Equals<\/a> would suffice.<\/p>\n<p>Key objects must be immutable (unable to change ex&#8211;string) as long as they are used as keys in the Hashtable.<\/p>\n<p>When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element.<\/p>\n<p>The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size. A different load factor can also be specified when the Hashtable is created.<\/p>\n<p>As elements are added to a Hashtable, the actual load factor of the Hashtable increases. When the actual load factor reaches the specified load factor, the number of buckets in the Hashtable is automatically increased to the smallest prime number that is larger than twice the current number of Hashtable buckets.<\/p>\n<p>Each key object in the Hashtable must provide its own hash function, which can be accessed by calling <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable.gethash(v=vs.110).aspx\">GetHash<\/a>. However, any object implementing <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ihashcodeprovider(v=vs.110).aspx\">IHashCodeProvider<\/a> can be passed to a Hashtable constructor, and that hash function is used for all objects in the table.<\/p>\n<p>The capacity of a Hashtable is the number of elements the Hashtable can hold. As elements are added to a Hashtable, the capacity is automatically increased as required through reallocation.<\/p>\n<p>For very large Hashtable objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh285054(v=vs.110).aspx\">gcAllowVeryLargeObjects<\/a> configuration element to true in the run-time environment.<\/p>\n<p>The foreach statement of the C# language (For Each in Visual Basic) requires the type of each element in the collection. Since each element of the Hashtable is a key\/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.dictionaryentry(v=vs.110).aspx\">DictionaryEntry<\/a>. For example:<\/p>\n<p>foreach(DictionaryEntry de in myHashtable)<\/p>\n<p>{<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>}<\/p>\n<p>The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.<\/p>\n<p>Because serializing and deserializing an enumerator for a Hashtable can cause the elements to become reordered, it is not possible to continue enumeration without calling the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator.reset(v=vs.110).aspx\">Reset<\/a> method.<\/p>\n<table border=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td><b><br \/>\n<\/b><b>Note<\/b><\/td>\n<\/tr>\n<tr>\n<td>Because \u00a0 keys can be inherited and their behavior changed, their absolute uniqueness \u00a0 cannot be guaranteed by comparisons using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.type.equals(v=vs.110).aspx\">Equals<\/a> method.<\/p>\n<pre>using System;<\/pre>\n<pre>using System.Collections;<\/pre>\n<pre><\/pre>\n<pre>class Example<\/pre>\n<pre>{<\/pre>\n<pre>\u00a0\u00a0\u00a0 public\u00a0static\u00a0void Main()<\/pre>\n<pre>\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Create a new hash table.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Hashtable openWith = new Hashtable();<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Add some elements to the hash table. There are no<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ duplicate keys, but some of the values are duplicates.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Add(\"txt\", \"notepad.exe\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Add(\"bmp\", \"paint.exe\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Add(\"dib\", \"paint.exe\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Add(\"rtf\", \"wordpad.exe\");<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The Add method throws an exception if the new key is<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ already in the hash table.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Add(\"txt\", \"winword.exe\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"An element with Key = \"txt\" already exists.\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The Item property is the default property, so you<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ can omit its name when accessing elements.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Console.WriteLine(\"For key = \"rtf\", value = {0}.\", openWith[\"rtf\"]);<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The default Item property can be used to change the value<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ associated with a key.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith[\"rtf\"] = \"winword.exe\";<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"For key = \"rtf\", value = {0}.\", openWith[\"rtf\"]);<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ If a key does not exist, setting the default Item property<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ for that key adds a new key\/value pair.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith[\"doc\"] = \"winword.exe\";<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ ContainsKey can be used to test keys before inserting<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ them.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (!openWith.ContainsKey(\"ht\"))<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Add(\"ht\", \"hypertrm.exe\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"Value added for key = \"ht\": {0}\", openWith[\"ht\"]);<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ When you use foreach to enumerate hash table elements,<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ the elements are retrieved as KeyValuePair objects.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine();<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach( DictionaryEntry de in openWith )<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"Key = {0}, Value = {1}\", de.Key, de.Value);<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ To get the values alone, use the Values property.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ICollection valueColl = openWith.Values;<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The elements of the ValueCollection are strongly typed<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ with the type that was specified for hash table values.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine();<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach( string s in valueColl )<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"Value = {0}\", s);<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ To get the keys alone, use the Keys property.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ICollection keyColl = openWith.Keys;<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The elements of the KeyCollection are strongly typed<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ with the type that was specified for hash table keys.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine();<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach( string s in keyColl )<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"Key = {0}\", s);<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Use the Remove method to remove a key\/value pair.<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"nRemove(\"doc\")\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 openWith.Remove(\"doc\");<\/pre>\n<pre><\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (!openWith.ContainsKey(\"doc\"))<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Console.WriteLine(\"Key \"doc\" is not found.\");<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre>\u00a0\u00a0\u00a0 }<\/pre>\n<pre>}<\/pre>\n<pre><\/pre>\n<pre>\/* This code example produces the following output:<\/pre>\n<pre><\/pre>\n<pre>An element with Key = \"txt\" already exists.<\/pre>\n<pre>For key = \"rtf\", value = wordpad.exe.<\/pre>\n<pre>For key = \"rtf\", value = winword.exe.<\/pre>\n<pre>Value added for key = \"ht\": hypertrm.exe<\/pre>\n<pre><\/pre>\n<pre>Key = dib, Value = paint.exe<\/pre>\n<pre>Key = txt, Value = notepad.exe<\/pre>\n<pre>Key = ht, Value = hypertrm.exe<\/pre>\n<pre>Key = bmp, Value = paint.exe<\/pre>\n<pre>Key = rtf, Value = winword.exe<\/pre>\n<pre>Key = doc, Value = winword.exe<\/pre>\n<pre><\/pre>\n<pre>Value = paint.exe<\/pre>\n<pre>Value = notepad.exe<\/pre>\n<pre>Value = hypertrm.exe<\/pre>\n<pre>Value = paint.exe<\/pre>\n<pre>Value = winword.exe<\/pre>\n<pre>Value = winword.exe<\/pre>\n<pre><\/pre>\n<pre>Key = dib<\/pre>\n<pre>Key = txt<\/pre>\n<pre>Key = ht<\/pre>\n<pre>Key = bmp<\/pre>\n<pre>Key = rtf<\/pre>\n<pre>Key = doc<\/pre>\n<pre><\/pre>\n<pre>Remove(\"doc\")<\/pre>\n<pre>Key \"doc\" is not found.<\/pre>\n<pre> *\/<\/pre>\n<p>So here we go :<\/p>\n<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td><b>Hashtable<\/b><\/td>\n<td><b>Dictionary<\/b><\/td>\n<td><b>Hashset<\/b><\/td>\n<\/tr>\n<tr>\n<td>Represents a collection of key\/value pairs \u00a0\u00a0\u00a0 that are organized based on the hash code of the key.<\/td>\n<td>Represents a collection of keys and \u00a0\u00a0\u00a0 values. Dictionary&lt;TKey, TValue&gt; class is implemented as a hash \u00a0\u00a0\u00a0 table..<\/td>\n<td>HashSet class is an unordered collection \u00a0\u00a0\u00a0 for containing unique elements.This class is based on the model of mathematical sets and \u00a0\u00a0\u00a0 provides high-performance set operations<\/td>\n<\/tr>\n<tr>\n<td>Each element is a key\/value pair stored in \u00a0\u00a0\u00a0 a <span style=\"text-decoration:underline;\">DictionaryEntry<\/span> object.<\/td>\n<td>Each element type is a <span style=\"text-decoration:underline;\">KeyValuePair&lt;TKey, \u00a0\u00a0\u00a0 TValue&gt;<\/span> of the key type and the value type<\/td>\n<td>Each element type is the type of T defined \u00a0\u00a0\u00a0 at the time of the creation of HashSet<\/td>\n<\/tr>\n<tr>\n<td>Hashtable are of type Object; therefore, \u00a0\u00a0\u00a0 boxing and unboxing typically occur when you store or retrieve a value type<\/td>\n<td>A Dictionary&lt;TKey, TValue&gt; of a \u00a0\u00a0\u00a0 specific type (other than Object) provides better performance than a \u00a0\u00a0\u00a0 Hashtable for value types<\/td>\n<td>A HashSet&lt;T&gt; collection is not \u00a0\u00a0\u00a0 sorted and cannot contain duplicate elements.If order or element duplication is more important than \u00a0\u00a0\u00a0 performance for your application, consider using the List&lt;T&gt; class \u00a0\u00a0\u00a0 together with the Sort method.<\/td>\n<\/tr>\n<tr>\n<td>Hashtable implements IDictionary&lt;TKey, \u00a0\u00a0\u00a0 TValue&gt;<\/td>\n<td>Dictionary implements IDictionary&lt;TKey, \u00a0\u00a0\u00a0 TValue&gt;,<\/td>\n<td>Starting with the .NET Framework version \u00a0\u00a0\u00a0 4, the HashSet&lt;T&gt; class implements the ISet&lt;T&gt; class<\/td>\n<\/tr>\n<tr>\n<td>Hashtable is thread safe for use by \u00a0\u00a0\u00a0 multiple reader threads and a single writing thread. It is thread safe for \u00a0\u00a0\u00a0 multi-thread use when only one of the threads perform write (update) \u00a0\u00a0\u00a0 operations<\/td>\n<td>A Dictionary&lt;TKey, TValue&gt; can \u00a0\u00a0\u00a0 support multiple readers concurrently, as long as the collection is not \u00a0\u00a0\u00a0 modified.<\/td>\n<td>Any public static members of this type are \u00a0\u00a0\u00a0 thread safe. Any instance members are not guaranteed to be thread safe.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">HashTable Represents a collection of key\/value pairs that are organized based on the hash code of the key. Hashtable optimizes lookups. It computes an adding hash of each key. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type. Dictionary A dictionary&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=200\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}