Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to create a truely weak-keyed dictionary in C#?
    primarykey
    data
    text
    <p>I'm trying to nut out the details for a true <code>WeakKeyedDictionary&lt;,&gt;</code> for C#... but I'm running into difficulties.</p> <p>I realise this is a non-trivial task, but the seeming inability to declare a <code>WeakKeyedKeyValuePair&lt;,&gt;</code> (where the GC only follows the value reference if the key is reachable) makes it seemingly impossible.</p> <p>There are two main problems I see:</p> <ol> <li><p>Every implementation I've so far seen does not trim values after keys have been collected. Think about that - one of the main reasons for using such a Dictionary is to prevent those values being kept around (not just the keys!) as they're unreachable, but here they are left pointed to by strong references.</p> <p>Yes, add/remove from the Dictionary enough and they'll eventually be replaced, but what if you don't?</p></li> <li><p>Without a hypothetical <code>WeakKeyedKeyValuePair&lt;,&gt;</code> (or another means of telling the GC to only mark the value if the key is reachable) any value that refers to it's key would never be collected. This is a problem when storing arbitrary values.</p></li> </ol> <p>Problem 1 could be tackled in a fairly non-ideal/hackish way : use GC Notifications to wait for a full GC to complete, and then go along and prune the dictionary in another thread. This one I'm semi-ok with.</p> <p>But problem 2 has me stumped. I realise this is easily countered by a "so don't do that", but it has me wondering - is this problem even possible to solve?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload