Class AsyncLazy<T>
Provides a mechanism for a lazily initializable value that is created asynchronously.
public sealed class AsyncLazy<T>
Type Parameters
TThe type of the lazily initializable value.
- Inheritance
-
AsyncLazy<T>
- Inherited Members
- Extension Methods
Remarks
This type caches the Task<TResult> returned by the factory. The factory is invoked at most once unless ClearValue() is called.
Constructors
AsyncLazy(Func<Task<T>>)
Initializes a new instance of the AsyncLazy<T> class from an async factory method.
public AsyncLazy(Func<Task<T>> factory)
Parameters
AsyncLazy(T)
Initializes a new instance of the AsyncLazy<T> class from a given value.
public AsyncLazy(T value)
Parameters
valueTThe value to initialize this instance with.
Properties
IsValueCreated
Determines whether the value has been created (i.e. the factory was invoked) or not.
public bool IsValueCreated { get; }
Property Value
ValueTaskOrDefault
Gets the cached Task<TResult> if the value has already been requested, or null otherwise.
public Task<T>? ValueTaskOrDefault { get; }
Property Value
- Task<T>
Methods
ClearValue()
Clears the cached value. Upon the next call of GetValueAsync(), the value will be lazily initialized again.
public void ClearValue()
GetValueAsync()
Gets the lazily initializable value asynchronously. If the value has not been created yet, this method activates its initialization.
public Task<T> GetValueAsync()
Returns
- Task<T>