1) The provided C# example fails to compile due to a conversion error. Example fixes:
A) Replace cwt.TryGetValue(wr2.Target, out data) with cwt.TryGetValue((ManagedClass)wr2.Target, out data)
B) Replace var cwt = new ConditionalWeakTable<ManagedClass, ClassData>(); with var cwt = new ConditionalWeakTable<object, ClassData>();
2) Possibly it's worth adding a note that the example may fail to function in debug mode, since in-scope variables are ineligible for collection during debug.
Type of issue
Code doesn't work
Description
1) The provided C# example fails to compile due to a conversion error. Example fixes: A) Replace
cwt.TryGetValue(wr2.Target, out data)
withcwt.TryGetValue((ManagedClass)wr2.Target, out data)
B) Replacevar cwt = new ConditionalWeakTable<ManagedClass, ClassData>();
withvar cwt = new ConditionalWeakTable<object, ClassData>();
2) Possibly it's worth adding a note that the example may fail to function in debug mode, since in-scope variables are ineligible for collection during debug.
Page URL
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.conditionalweaktable-2?view=net-8.0&redirectedfrom=MSDN
Content source URL
https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Runtime.CompilerServices/ConditionalWeakTable`2.xml
Document Version Independent Id
6a9b0e31-d0a6-c930-7959-20ac6f03ec4a
Article author
@dotnet-bot