Note that there are some explanatory texts on larger screens.

plurals
  1. POShould ConditionalWeakTable<TKey, TValue> be used for non-compiler purposes?
    primarykey
    data
    text
    <p>I've recently come across the <a href="http://msdn.microsoft.com/en-us/library/dd287757.aspx" rel="nofollow noreferrer"><code>ConditionalWeakTable&lt;TKey,TValue&gt;</code></a> class in my search for an <code>IDictionary</code> which uses weak references, as suggested in answers <a href="https://stackoverflow.com/questions/2784291/good-implementation-of-weak-dictionary-in-net">here</a> and <a href="https://stackoverflow.com/questions/5764556/best-time-to-cull-weakreferences-in-a-collection-in-net/5764855#5764855">here</a>.</p> <p>There is <a href="http://blogs.msdn.com/b/dotnet/archive/2009/05/18/the-conditional-weak-table-enabling-dynamic-object-properties.aspx" rel="nofollow noreferrer">a definitive MSDN article</a> which introduced the class and which states:</p> <blockquote> <p>You can find the class ... in the System.Runtime.CompilerServices namespace. It’s in CompilerServices because it’s not a general-purpose dictionary type: we intend for it to only be used by compiler writers.</p> </blockquote> <p>and later again:</p> <blockquote> <p>...the conditional weak table is not intended to be a general purpose collection... But if you’re writing a .NET language of your own and need to expose the ability to attach properties to objects you should definitely look into the Conditional Weak Table.</p> </blockquote> <p>In line with this, the MSDN entry description of the class reads:</p> <blockquote> <p>Enables compilers to dynamically attach object fields to managed objects.</p> </blockquote> <p>So obviously it was originally created for a very specific purpose - to help the DLR, and the <code>System.Runtime.CompilerServices</code> namespace embodies this. But it seems to have found a much wider use than that - even within the CLR. If I search for references of <a href="http://msdn.microsoft.com/en-us/library/dd287757.aspx" rel="nofollow noreferrer">ConditionalWeakTable</a> in <a href="http://wiki.sharpdevelop.net/ILSpy.ashx" rel="nofollow noreferrer">ILSpy</a>, for example, I can see that is used in the MEF class <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.hosting.catalogexportprovider.aspx" rel="nofollow noreferrer"><code>CatalogExportProvider</code></a> and in the internal WPF <code>DataGridHelper</code> class, amongst others.</p> <p>My question is whether it is okay to use <a href="http://msdn.microsoft.com/en-us/library/dd287757.aspx" rel="nofollow noreferrer">ConditionalWeakTable</a> outside of compiler writing and language tools, and whether there is any risk in doing so in terms of incurring additional overhead or of the implementation changing significantly in future .NET versions. (Or should it be avoided and a custom implementation like <a href="http://blogs.msdn.com/b/nicholg/archive/2006/06/04/617466.aspx" rel="nofollow noreferrer">this one</a> be used instead).</p> <p>There is also further reading <a href="http://weblog.ikvm.net/PermaLink.aspx?guid=7f47ad08-cdef-4dc2-b2fd-5dfdc1baf11d" rel="nofollow noreferrer">here</a>, <a href="http://blog.gx.weltkante.de/2011/06/true-weak-keyed-dictionary.html" rel="nofollow noreferrer">here</a> and <a href="https://stackoverflow.com/questions/8441055/is-it-possible-to-create-a-truely-weak-keyed-dictionary-in-c">here</a> about how the <a href="http://msdn.microsoft.com/en-us/library/dd287757.aspx" rel="nofollow noreferrer">ConditionalWeakTable</a> makes use of a hidden CLR implementation of <a href="http://en.wikipedia.org/wiki/Ephemeron" rel="nofollow noreferrer">ephemerons</a> (via <code>System.Runtime.Compiler.Services. DependentHandle</code>) to deal with the problem of cycles between keys and values, and how this cannot easily be accomplished in a custom manner.</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.
    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