{"id":374,"date":"2023-08-03T06:00:40","date_gmt":"2023-08-03T06:00:40","guid":{"rendered":"http:\/\/192.168.0.142\/?p=374"},"modified":"2023-08-03T07:16:28","modified_gmt":"2023-08-03T07:16:28","slug":"difference-between-hashtable-and-dictionary-in-c","status":"publish","type":"post","link":"http:\/\/192.168.0.142\/difference-between-hashtable-and-dictionary-in-c\/","title":{"rendered":"Difference between Hashtable and Dictionary in C#"},"content":{"rendered":"\n
Hashtable hashtable = new Hashtable();\n hashtable.Add(\"Area\", 1000);\n int value = (int)hashtable[\"Area\"];<\/code><\/pre>\n\n\n\n\n- A Hashtable object consists of buckets that contain the elements of the collection. A bucket is a virtual subgroup of elements within the Hashtable, which makes searching and retrieving easier and faster than in most collections.<\/li>\n\n\n\n
- Each bucket is associated with a hash code, generated using a hash function and based on the key of the element.<\/li>\n\n\n\n
- When an object is added to a Hashtable, it is stored in the bucket that is associated with the hash code that matches the object’s hash code. When a value is being searched for in the Hashtable, the hash code is generated for that value, and the bucket associated with that hash code is searched.<\/li>\n\n\n\n
- A Hash table is made up of a mapping function and an array.<\/li>\n\n\n\n
- The array contains your data, while the mapping function is used to assign numerical values(keys) to the data.<\/li>\n\n\n\n
- This helps in categorizing the data, which speeds up search times when you search for it.<\/li>\n\n\n\n
- A hash function should assign unique keys to each bucket (data slot), but in practice a pair of keys might get hashed to the same table. This is known as a hash collision, and the design of the hash table must be able to take this probability into account.<\/li>\n<\/ul>\n\n\n\n
Dictionary<\/h3>\n\n\n\n\n- A dictionary is used where fast lookups are critical. The Dictionary type provides fast lookups with keys to get values.<\/li>\n\n\n\n
- It returns error if we try to find a key which does not exist.<\/li>\n\n\n\n
- It is faster than a Hashtable because there is no boxing and unboxing.<\/li>\n\n\n\n
- Dictionary is a generic type which means we can use it with any data type.<\/li>\n<\/ul>\n\n\n\n
Dictionary<string, int> d = new Dictionary<string, int>();\n foreach (KeyValuePair<string, int> pair in d)\n {\n Console.WriteLine (\"{0}, {1}\",pair.Key, pair.Value);\n }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Hashtable Dictionary<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[33,36,35],"_links":{"self":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/374"}],"collection":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":5,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":425,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/374\/revisions\/425"}],"wp:attachment":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}