Show / Hide Table of Contents

Class AsyncLazy<T>

Provides support for asynchronous lazy initialization.

Inheritance
object
AsyncLazy<T>
Implements
ISupplier<CancellationToken, Task<T>>
IFunctional
IResettable
Inherited Members
object.GetType()
object.MemberwiseClone()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncLazy<T> : ISupplier<CancellationToken, Task<T>>, IFunctional, IResettable
Type Parameters
Name Description
T

The type of object that is being asynchronously initialized.

Constructors

View Source

AsyncLazy(Func<CancellationToken, Task<T>>, bool)

Initializes a new instance of lazy value.

Declaration
public AsyncLazy(Func<CancellationToken, Task<T>> valueFactory, bool resettable = false)
Parameters
Type Name Description
Func<CancellationToken, Task<T>> valueFactory

The function used to compute actual value.

bool resettable

true if previously computed value can be removed and computation executed again when it will be requested; false if value can be computed exactly once.

Exceptions
Type Condition
ArgumentException

valueFactory is null.

View Source

AsyncLazy(T)

Initializes a new instance of lazy value which is already computed.

Declaration
public AsyncLazy(T value)
Parameters
Type Name Description
T value

Already computed value.

Properties

View Source

IsValueCreated

Gets a value that indicates whether a value has been computed.

Declaration
public bool IsValueCreated { get; }
Property Value
Type Description
bool
View Source

Value

Gets value if it is already computed.

Declaration
public Result<T>? Value { get; }
Property Value
Type Description
Result<T>?

Methods

View Source

Reset()

Removes already computed value from the current object.

Declaration
public bool Reset()
Returns
Type Description
bool

true if previous value is removed successfully; false if value is still computing or this instance is not resettable.

View Source

ToString()

Returns textual representation of this object.

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

The string representing this object.

Overrides
object.ToString()
View Source

WithCancellation(CancellationToken)

Gets already completed task or invokes the factory.

Declaration
public Task<T> WithCancellation(CancellationToken token)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
Task<T>

Lazy representation of the value.

Implements

ISupplier<T, TResult>
IFunctional
IResettable

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
☀
☾