Edit

Share via


DependentHandle Struct

Definition

Represents a dependent garbage-collection handle. The handle will conditionally keep a dependent object instance alive as long as a target object instance is alive as well, without representing a strong reference to the target instance.

public value class DependentHandle : IDisposable
public struct DependentHandle : IDisposable
type DependentHandle = struct
    interface IDisposable
Public Structure DependentHandle
Implements IDisposable
Inheritance
DependentHandle
Implements

Remarks

A DependentHandle value with a given object instance as target will not cause the target to be kept alive if there are no other strong references to it, but it will do so for the dependent object instance as long as the target is alive.

Using this type is conceptually equivalent to having a weak reference to a given target object instance A, with that object having a field or property (or some other strong reference) to a dependent object instance B.

The DependentHandle type is not thread-safe, and consumers are responsible for ensuring that Dispose() is not called concurrently with other APIs. Not doing so results in undefined behavior.

The IsAllocated, Target, Dependent, and TargetAndDependent properties are instead thread-safe, and safe to use if Dispose() is not concurrently invoked as well.

Constructors

DependentHandle(Object, Object)

Initializes a new instance of the DependentHandle struct with the specified arguments.

Properties

Dependent

Gets or sets the dependent object instance for the current handle.

IsAllocated

Gets a value indicating whether this instance was constructed with DependentHandle(Object, Object) and has not yet been disposed.

Target

Gets or sets the target object instance for the current handle. The target can only be set to a null value once the DependentHandle instance has been created. Doing so will cause Dependent to start returning null as well, and to become eligible for collection even if the previous target is still alive.

TargetAndDependent

Gets the values of both Target and Dependent (if available) as an atomic operation. That is, even if Target is concurrently set to null, calling this method will either return null for both target and dependent, or return both previous values. If Target and Dependent were used sequentially in this scenario instead, it would be possible to sometimes successfully retrieve the previous target, but then fail to get the dependent.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Applies to