Class SoftReference<T>
Represents a form of weak reference which is eligible for garbage collection in Generation 2 only.
Inherited Members
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 SourceSoftReference(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 SourceTargetAndState
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
Methods
| Edit this page View SourceClear()
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.
~SoftReference()
Makes the referenced object available for garbage collection (if not referenced elsewhere).
Declaration
protected ~SoftReference()
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
| Edit this page View SourceTryGetTarget()
Tries to retrieve the target object.
Declaration
public Optional<T> TryGetTarget()
Returns
Type | Description |
---|---|
Optional<T> | The referenced object. |
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 |
Operators
| Edit this page View Sourceexplicit 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. |
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. |