Francois C Francois C - 1 year ago 81
C# Question

How to retrieve actual item from HashSet<T>?

I've read this question about why it is not possible, but haven't found a solution to the problem.

I would like to retrieve an item from a .NET

. I'm looking for a method that would have this signature:

/// <summary>
/// Determines if this set contains an item equal to <paramref name="item"/>,
/// according to the comparison mechanism that was used when the set was created.
/// The set is not changed. If the set does contain an item equal to
/// <paramref name="item"/>, then the item from the set is returned.
/// </summary>
bool TryGetItem<T>(T item, out T foundItem);

Searching the set for an item with such a method would be O(1). The only way to retrieve an item from a
is to enumerate all items which is O(n).

I haven't find any workaround to this problem other then making my own
or use a
Dictionary<K, V>
. Any other idea?


I don't want to check if the
contains the item. I want to get the reference to the item that is stored in the
because I need to update it (without replacing it by another instance). The item I would pass to the
would be equal (according to the comparison mechanism that I've passed to the constructor) but it would not be the same reference.

Answer Source

This is actually a huge omission in the set of collections. You would need either a Dictionary of keys only or a HashSet that allows for the retrieval of object references. So many people have asked for it, why it doesn't get fixed is beyond me.

Without third-party libraries the best workaround is to use Dictionary<T, T> with keys identical to values, since Dictionary stores its entries as a hash table. Performance-wise it is the same as the HashSet, but it wastes memory of course (size of a pointer per entry).

Dictionary<T, T> myHashedCollection;
    item = myHashedCollection[item]; //replace duplicate
    myHashedCollection.Add(item, item); //add previously unknown item
//work with unique item
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download