Ferrous API Reference

All Types / Ferrous.Concurrency

class OnceLock<T>

public sealed class OnceLock<T>

A thread-safe write-once synchronization cell inspired by Rust's std::sync::OnceLock<T>. Safe to store in static readonly fields without triggering FE0501.

Type Parameters
T
The stored value type.

Properties

public bool IsInitialized

Gets a value indicating whether the cell has been initialized.

Methods

public bool TryGet([MaybeNullWhen(false)] out T value)

Attempts to read the initialized value without blocking.

public bool TrySet(T value)

Attempts to initialize the cell with value. Returns false if already initialized.

public T GetOrInit(Func<T> factory)

Returns the initialized value, invoking factory at most once if uninitialized.