Soenneker.Atomics.ValueNullableLazys
4.0.12
Prefix Reserved
dotnet add package Soenneker.Atomics.ValueNullableLazys --version 4.0.12
NuGet\Install-Package Soenneker.Atomics.ValueNullableLazys -Version 4.0.12
<PackageReference Include="Soenneker.Atomics.ValueNullableLazys" Version="4.0.12" />
<PackageVersion Include="Soenneker.Atomics.ValueNullableLazys" Version="4.0.12" />
<PackageReference Include="Soenneker.Atomics.ValueNullableLazys" />
paket add Soenneker.Atomics.ValueNullableLazys --version 4.0.12
#r "nuget: Soenneker.Atomics.ValueNullableLazys, 4.0.12"
#:package Soenneker.Atomics.ValueNullableLazys@4.0.12
#addin nuget:?package=Soenneker.Atomics.ValueNullableLazys&version=4.0.12
#tool nuget:?package=Soenneker.Atomics.ValueNullableLazys&version=4.0.12
Soenneker.Atomics.ValueNullableLazys
Provides inline lazy storage that distinguishes an uninitialized value from a successfully initialized null result.
Install
dotnet add package Soenneker.Atomics.ValueNullableLazys
Usage
Use this variant when null is a valid, cacheable factory result:
using Soenneker.Atomics.ValueLocks;
using Soenneker.Atomics.ValueNullableLazys;
public sealed class UserLookup
{
private ValueAtomicLock _sync;
private ValueNullableLazy<User> _user;
public User? Get(string id) => _user.GetOrCreate(
ref _sync,
id,
static userId => FindUser(userId));
}
After the factory returns—even when it returns null—IsValueCreated is true and later calls reuse that result. TryGetValue distinguishes the states: a false return means uninitialized; a true return with a null output means initialized-to-null.
GetOrCreate runs one factory under the supplied lock and retries after exceptions. GetOrCreatePublicationOnly may run duplicate factories during a race, while GetOrCreateUnsafe requires external synchronization.
Keep both mutable structs in fields. Copies have independent lazy state or lock domains. Publication-only factories should be side-effect free, and losing non-null candidates are not disposed automatically.
What you get
ValueNullableLazy<T>— Provides inline lazy storage that distinguishes an uninitialized value from a successfully initializednullresult.
API at a glance
| API | What it does | Result / important behavior |
|---|---|---|
ValueNullableLazy<T>.IsValueCreated |
Gets a value indicating whether initialization has completed successfully, including when the result was null. | Gets a value indicating whether initialization has completed successfully, including when the result was null. |
ValueNullableLazy<T>.TryGetValue(value) |
Attempts to read the initialized value without invoking a factory. A true return value with a null output means initialization completed with null. | true if the requested update was applied; otherwise, false. |
ValueNullableLazy<T>.GetOrCreate(sync, factory) |
Gets the initialized value or invokes factory exactly once using execution-and-publication semantics. |
The requested value. |
ValueNullableLazy<T>.GetOrCreate(sync, state, factory) |
Gets the initialized value or invokes factory exactly once using execution-and-publication semantics. Supplying state allows callers to use a static factory and avoid a closure allocation. |
The requested value. |
ValueNullableLazy<T>.GetOrCreateUnsafe(factory) |
Gets or creates the value without locking. This method is only safe when the caller provides external synchronization or guarantees single-threaded access. | The requested value. |
ValueNullableLazy<T>.GetOrCreateUnsafe(state, factory) |
Gets or creates the value without locking. Supplying state allows callers to use a static factory and avoid a closure allocation. | The requested value. |
ValueNullableLazy<T>.GetOrCreatePublicationOnly(factory) |
Gets the initialized value or atomically publishes one factory result. During a race the factory may run more than once, but every caller observes the single published result. | The requested value. |
ValueNullableLazy<T>.GetOrCreatePublicationOnly(state, factory) |
Gets the initialized value or atomically publishes one factory result. Supplying state allows callers to use a static factory and avoid a closure allocation. | The requested value. |
Important behavior
ValueNullableLazy<T>: The default value is ready to use and occupies one reference-sized field. AValueAtomicLockcan be shared by several lazy fields on the same owner, avoiding a separate synchronization object for every value. This is a mutablestructintended for use as a private field. Avoid copying it because each copy has independent initialization state. Exceptions thrown by a factory are not cached, and a later call may retry initialization.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Soenneker.Atomics.ValueLocks (>= 4.0.10)
-
net9.0
- Soenneker.Atomics.ValueLocks (>= 4.0.10)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Atomics.ValueNullableLazys:
| Package | Downloads |
|---|---|
|
Soenneker.Reflection.Cache
The fastest .NET Reflection cache |
GitHub repositories
This package is not used by any popular GitHub repositories.