Soenneker.Atomics.ValueLazys
4.0.11
Prefix Reserved
dotnet add package Soenneker.Atomics.ValueLazys --version 4.0.11
NuGet\Install-Package Soenneker.Atomics.ValueLazys -Version 4.0.11
<PackageReference Include="Soenneker.Atomics.ValueLazys" Version="4.0.11" />
<PackageVersion Include="Soenneker.Atomics.ValueLazys" Version="4.0.11" />
<PackageReference Include="Soenneker.Atomics.ValueLazys" />
paket add Soenneker.Atomics.ValueLazys --version 4.0.11
#r "nuget: Soenneker.Atomics.ValueLazys, 4.0.11"
#:package Soenneker.Atomics.ValueLazys@4.0.11
#addin nuget:?package=Soenneker.Atomics.ValueLazys&version=4.0.11
#tool nuget:?package=Soenneker.Atomics.ValueLazys&version=4.0.11
Soenneker.Atomics.ValueLazys
Provides inline lazy storage for a non-null reference value without allocating a Lazy{T} wrapper.
Install
dotnet add package Soenneker.Atomics.ValueLazys
Usage
Store both mutable structs as fields and pass the lock by reference:
using System.Text.RegularExpressions;
using Soenneker.Atomics.ValueLazys;
using Soenneker.Atomics.ValueLocks;
public sealed class InputValidator
{
private ValueAtomicLock _sync;
private ValueLazy<Regex> _pattern;
public Regex Pattern => _pattern.GetOrCreate(
ref _sync,
static () => new Regex("^[a-z0-9-]+$"));
}
GetOrCreate provides execution-and-publication: one caller runs the factory while competitors wait on the supplied lock. Exceptions are not cached, so a later call retries.
GetOrCreatePublicationOnly avoids the lock but may run the factory concurrently several times; only one non-null reference wins publication. Use it only when duplicate construction is safe and abandoned candidates need no disposal. GetOrCreateUnsafe requires external synchronization or single-threaded access.
Do not copy ValueLazy<T> or the accompanying ValueAtomicLock. Copies establish independent storage or lock domains and defeat initialization coordination.
What you get
ValueLazy<T>— Provides inline lazy storage for a non-null reference value without allocating aLazy{T}wrapper.
API at a glance
| API | What it does | Result / important behavior |
|---|---|---|
ValueLazy<T>.IsValueCreated |
Gets a value indicating whether initialization has completed successfully. | Gets a value indicating whether initialization has completed successfully. |
ValueLazy<T>.TryGetValue(value) |
Attempts to read the initialized value without invoking a factory. | true if the requested update was applied; otherwise, false. |
ValueLazy<T>.GetOrCreate(sync, factory) |
Gets the initialized value or invokes factory exactly once using execution-and-publication semantics. |
The requested value. |
ValueLazy<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. |
ValueLazy<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. |
ValueLazy<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. |
ValueLazy<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 receives the single published value. | The requested value. |
ValueLazy<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
ValueLazy<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.ValueLazys:
| Package | Downloads |
|---|---|
|
Soenneker.Reflection.Cache
The fastest .NET Reflection cache |
GitHub repositories
This package is not used by any popular GitHub repositories.