Is there a C# class that provides map with weak keys or/and weak values? Or at least WeakHashMap like functionality.
3 Answers
In .Net 3.5 and below, there is no such structure available. However I wrote up one for a side project and posted the code at the following location.
Starting .NET 4.0, there is a structure available called ConditionalWeakTable
in the Runtime.CompilerServices namespace that also does the trick.

- 98,240
- 88
- 296
- 433

- 733,204
- 149
- 1,241
- 1,454
-
I recently came across this blog post It states "I'll save [compaction] for next time". I'm interested how compaction would be done, but I could not find "next time" :-( is it available anywhere? Here is what I came up with on my own: http://stackoverflow.com/questions/2047591 – dtb Jan 17 '10 at 16:39
-
Note that `ConditionalWeakTable` is actually stronger than `WeakHashMap`. `WeakHashMap` persists values and everything reachable from them, so if a key in the `WeakHashMap` can be reached from its value, it will be persisted even though the hash map itself holds only weak references to keys. On the other hand, if a key can only be reached from a value in the `ConditionalWeakTable`, it will not be persisted so the whole key-value pair will be eligible for garbage collection. – GregRos Jul 26 '17 at 14:28
Prior to .NET 4, the CLR did not provide the functionality necessary to implement a map of this form. In particular, Java provides the ReferenceQueue<T>
class, which WeakHashMap
uses to manage the weak keys in the map. Since there is no equivalent to this class in .NET, there is no clean way to build an equivalent Dictionary
.
In .NET 4, a new class ConditionalWeakTable<TKey, TValue>
was added as part of an effort to improve the ability of the CLR to support dynamic languages. This class uses a new type of garbage collection handle, which is implemented within the CLR itself and exposed in mscorlib.dll through the internal DependentHandle
structure.
This means the following for you:
- There is no equivalent to
WeakHashMap
prior to .NET 4. - Starting with .NET 4, and continuing at least through .NET 4.5.1, the only way to support the functionality of
WeakHashMap
is to use theConditionalWeakTable
class (which is sealed).
Additional information is found in the following post:
Is it possible to create a truely weak-keyed dictionary in C#?

- 1
- 1

- 97,721
- 20
- 209
- 280
The closest platform equivalent is probably a Dictionary<K, WeakReference<V>>
. That is, it's just a regular dictionary, but with the values being weak references.

- 303,634
- 46
- 339
- 357
-
6You mixed something up, the keys have to be weak not the values, sorry just reread the question. – josefx Jan 17 '10 at 16:10
-
3It says "weak keys or/and weak values". Weak values are probably preferable to weak keys -- you don't want a weak key and a strong value, because then you don't have a way to get your value back if your key expires! – John Feminella Jan 17 '10 at 16:13
-
5Actually, weak keys are also very useful. For instance in caches. But they are harder to implement with just WeakReferences and Dictionary. – Daniel Sperry Jan 18 '10 at 17:40
-
That's very true. If this was a cache, though, I don't think a WeakHashMap is necessarily what you want -- there are some better alternatives. – John Feminella Jan 18 '10 at 17:57
-
For my current problem your solution is fine. I made a Dictionary
, and I might iterate over the key pairs from time to time to prune the collected values. However if I were more concerned with performance or concurrency this pruning would not be a such a good idea. WeakHashMap like (weak keys) also would not work. I asked the question trying to find the gold mine: some .net namespace with all nice gc aware collections and classes that I might need. In my previous project we built a STM system in java, there almost every possible combination of references were necessary. – Daniel Sperry Jan 19 '10 at 11:33 -
@John: This data structure is most commonly used to get the garbage collector to collect unreachable subgraphs in user-land graph data structures. When a key becomes unreachable and the dictionary is the only way to find its associated value then that value also becomes unreachable and so on. General collection of graphs a quite a hard problem otherwise, tantamount to implementing your own garbage collector. – J D Jan 17 '11 at 10:57
-
2-1: This may fall into one specific interpretation of the original post, but it definitely is not *anything* like the title, which asks for something equivalent to `WeakHashMap` in C#. Your dictionary uses strong references to the keys, which is nothing like `WeakHashMap`. – Sam Harwell Nov 09 '13 at 20:26
-
@280Z28: I hope you'll retract your -1 after reading the _very first sentence_ of the OP: "weak keys *and/or weak values*". The values are weak here, and as other commenters posted, no such structure existed for .NET < 4.0 at the time of the question. – John Feminella Nov 10 '13 at 05:24
-
@JohnFeminella Based on the wording of the question, I do not feel that the OP had a complete understanding of the behavior of `WeakHashMap` when asking this question. Based especially on the topic title, he appeared to be looking for a .NET class which should be used in situations similar to where `WeakHashMap` would be used in Java. On that interpretation, your answer is not helpful. – Sam Harwell Nov 10 '13 at 21:34
-
1@280Z28 I guess we have different interpretations of "helpful" given that this is the accepted answer, but note that you're always free to suggest edits to answers using the edit link. If you think there's a better way to express an existing answer, that's a good way to do it, rather than letting it languish in the comments (where fewer people are likely to see it). – John Feminella Nov 11 '13 at 19:36