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
Inherited Members
object.GetType()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Runtime
Assembly: DotNext.dll
Syntax
public sealed class SoftReference<T> : IOptionMonad<T>, ISupplier<object?>, IFunctional 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

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

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

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.

View Source

~SoftReference()

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

Declaration
protected ~SoftReference()
View Source

ToString()

Declaration
public override string? ToString()
Returns
Type Description
string
Overrides
object.ToString()
View Source

TryGetTarget()

Tries to retrieve the target object.

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

The referenced object.

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

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.

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

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾