I am trying to understand ConditionalWeakTable. What is the difference between
class ClassA
{
static readonly ConditionalWeakTable
The biggest difference between the two--indeed, the primary reason that ConditionalWeakTable exists and the reason it's part of CompilerServices--is that adding a field to ClassA requires the ability to add a field to ClassA, but constructing a ConditionalWeakTable with a key type of ClassA does not. Effectively, ConditionalWeakTable exists to allow code to effectively "add a field" to instances of any class without needing to modify the class itself. While it might in many cases be possible to use an identity-based weak-key dictionary for such a purpose, that would only work as long as no value held a direct or indirect reference to its key, and there were no cycles of keys which were referenced directly or directly via other key's values. The design of ConditionalWeakTable allows keys and values to be collected even when such cycles exist.