Search results
If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections) A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own ...
Jun 10, 2014 · take create a Dictionary<T> of your table entries (let's call it IEnumerable<T> (which ignores duplicate keys) create a Hashset<T> of the same IEnumerable<T> (which keeps duplicate keys, as long as the entire row isn't the same) and then iterate through dictionary.Values, calling hashset.Remove(value) for each value.
Jun 17, 2010 · 10. You use a hashtable (dictionary) when you want fast look up access to an item based on a key. If you are using List, IList or IEnumerable generally this means that you are looping over data (well in the case of IEnumerable it definitely means that), and a hashtable isn't going to net you anything.
Nov 19, 2008 · The Hashtable is a loosely-typed data structure, so you can add keys and values of any type to the Hashtable. The Dictionary class is a type-safe Hashtable implementation, and the keys and values are strongly typed. When creating a Dictionary instance, you must specify the data types for both the key and value.
Jul 27, 2015 · 131. Long after the question has been asked, so I don't expect to earn much rep. However I decided it would be fun to write my own very basic example (in less than 90 lines of code): public struct KeyValue<K, V>. public K Key { get; set; } public V Value { get; set; } public class FixedSizeGenericHashTable<K,V>. private readonly int size;
Apr 21, 2009 · The generic version of a Hashtable is the Dictionary<TKey,TValue> class (link). Here is some sample code translated from using a Hashtable into the most direct equivalent of Dictionary (argument checking removed for sake of brevity) HashTable table = new HashTable(); for ( int i = 0; i < keys.Length; i++ ) {.
Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs. Dictionary - same as above only strongly typed via generics, such as Dictionary<string, string> SortedList - a sorted generic list. Slowed on insertion since it has to figure out where to put things.
Dictionary faster than a Hashtable because there is no boxing and unboxing. Dictionary is a generic type which means we can use it with any data type. Hashtable: Hashtable returns null if we try to find a key which does not exist. Hashtable slower than dictionary because it requires boxing and unboxing.
Jan 31, 2018 · If you need to just find one key or one value, use the methods ContainsKey(object key) or ContainsValue(object value), both of which are found on the Hashtable type. Or you can go further and use linq extensions on the Hashtable parts: Hashtable t = new Hashtable(); t.Add("Key", "Adam"); // Get the key/value entries.
The simplest way I found to "sort" hashtable is: var hash = new Hashtable(); var orderedKeys = hash.Keys.Cast<string>().OrderBy(c => c); // supposing you're using string keys. var allKvp = from x in orderedKeys select new{ key = x, value = hash[x] }; However, Im not ordering the original hashtable, only reading its values in an ordered way.