Skip to content

Add ConditionalWeakTable.Remove(key, out value) polyfill #574

Description

@thomhurst

Is the feature request related to a problem

ConditionalWeakTable<TKey, TValue>.Remove(TKey, out TValue) is unavailable when targeting older frameworks such as netstandard2.0, although the one-argument Remove(TKey) and TryGetValue methods exist.

This prevents cross-target source such as:

if (!table.Remove(key, out var value))
{
    return false;
}

value.Complete();
return true;

Microsoft documents the overload here:

https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.conditionalweaktable-2.remove

Describe the solution

Add an extension polyfill with the BCL signature:

public static bool Remove<TKey, TValue>(
    this ConditionalWeakTable<TKey, TValue> target,
    TKey key,
    out TValue value)
    where TKey : class
    where TValue : class

A basic implementation can combine TryGetValue(key, out value) with Remove(key), resetting value when removal fails.

Describe alternatives considered

Consumers can perform TryGetValue followed by one-argument Remove themselves.

That fallback is not atomic: another thread could remove and re-add the key between operations. Locking on target does not synchronize callers using the BCL methods directly. Because the native overload performs lookup and removal under the table's internal lock, an extension cannot perfectly reproduce its concurrency semantics without runtime internals.

This issue asks whether Polyfill accepts that documented best-effort behavior, or has a more faithful approach for supported older runtimes.

Additional context

This surfaced in debug-only ownership diagnostics for a source-only pooling library targeting netstandard2.0. The common non-racing path is straightforward; atomicity is the only known semantic gap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions