Show / Hide Table of Contents

Class SoftReference<T>

Represents a form of weak reference which is eligible for garbage collection in Generation 2 only.

Inheritance
object
SoftReference<T>
Implements
IOptionMonad<T>
ISupplier<object>
IFunctional<Func<object>>
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.ReferenceEquals(object, object)
Namespace: DotNext.Runtime
Assembly: DotNext.dll
Syntax
public sealed class SoftReference<T> : IOptionMonad<T>, ISupplier<object?>, IFunctional<Func<object?>> where T : class
Type Parameters
Name Description
T

The type of the object referenced.

Remarks

The object that is soft-referenced survives the garbage collection in Generation 0 and 1. When GC is triggered for Generation 2, the object can be reclaimed according to SoftReferenceOptions. All public instance members of this type are thread-safe.

Constructors

| Edit this page View Source

SoftReference(T?, SoftReferenceOptions?)

Initializes a new soft reference.

Declaration
public SoftReference(T? target, SoftReferenceOptions? options = null)
Parameters
Type Name Description
T target

The object to be referenced.

SoftReferenceOptions options

The behavior of soft reference.

Properties

| Edit this page View Source

TargetAndState

Gets state of the referenced object and referenced object itself.

Declaration
public (T? Target, SoftReferenceState State) TargetAndState { get; }
Property Value
Type Description
(T Target, SoftReferenceState State)
Remarks

The returned target object is not null when the state is Strong or Weak.

Methods

| Edit this page View Source

Clear()

Makes the referenced object available for garbage collection (if not referenced elsewhere).

Declaration
public void Clear()
Remarks

This method stops tracking the referenced object. Thus, the object will be reclaimable by GC even if it is not reached Generation 2.

| Edit this page View Source

~SoftReference()

Makes the referenced object available for garbage collection (if not referenced elsewhere).

Declaration
protected ~SoftReference()
| Edit this page View Source

ToString()

Returns a string that represents the current object.

Declaration
public override string? ToString()
Returns
Type Description
string

A string that represents the current object.

Overrides
object.ToString()
| Edit this page View Source

TryGetTarget()

Tries to retrieve the target object.

Declaration
public Optional<T> TryGetTarget()
Returns
Type Description
Optional<T>

The referenced object.

| Edit this page View Source

TryGetTarget(out T?)

Tries to retrieve the target object.

Declaration
public bool TryGetTarget(out T? target)
Parameters
Type Name Description
T target

The referenced object.

Returns
Type Description
bool

true if the target was retrieved; otherwise, false.

Operators

| Edit this page View Source

explicit operator Optional<T>(SoftReference<T>?)

Tries to retrieve the target object.

Declaration
public static explicit operator Optional<T>(SoftReference<T>? reference)
Parameters
Type Name Description
SoftReference<T> reference

Soft reference.

Returns
Type Description
Optional<T>

The referenced object.

| Edit this page View Source

explicit operator T?(SoftReference<T>?)

Gets the referenced object.

Declaration
public static explicit operator T?(SoftReference<T>? reference)
Parameters
Type Name Description
SoftReference<T> reference

The reference to the object.

Returns
Type Description
T

The referenced object; or null if the object is not reachable.

Implements

IOptionMonad<T>
ISupplier<TResult>
IFunctional<TDelegate>

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾