Soenneker.Atomics.Strings 4.0.19

Prefix Reserved
dotnet add package Soenneker.Atomics.Strings --version 4.0.19
                    
NuGet\Install-Package Soenneker.Atomics.Strings -Version 4.0.19
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Atomics.Strings" Version="4.0.19" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Atomics.Strings" Version="4.0.19" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Atomics.Strings" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Atomics.Strings --version 4.0.19
                    
#r "nuget: Soenneker.Atomics.Strings, 4.0.19"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Atomics.Strings@4.0.19
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Atomics.Strings&version=4.0.19
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Atomics.Strings&version=4.0.19
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Atomics.Strings

A lock-free, thread-safe wrapper for atomically publishing and reading a string reference. Useful for shared string state and one-time (or racing) initialization without locks.

Install

dotnet add package Soenneker.Atomics.Strings

Usage

Publish a value only when no value exists:

using Soenneker.Atomics.Strings;

var currentEndpoint = new AtomicString();

if (currentEndpoint.TrySet("https://api.example.com"))
{
    // This caller won the null-to-value transition.
}

string? endpoint = currentEndpoint.Get();

GetOrAdd returns the already-published value or creates a candidate:

string instanceId = id.GetOrAdd(CreateInstanceId);

Under contention, multiple callers can run the factory; only one string is published and every caller returns the winner. Keep the factory side-effect free. It must not return null.

CompareExchange compares string references, not string contents. Equal strings held by different object references do not satisfy the comparand check. Use TrySet, Exchange, or an application-level loop when content equality is required.

What you get

  • AtomicString — A lock-free, thread-safe wrapper for atomically publishing and reading a string reference. Useful for shared string state and one-time (or racing) initialization without locks.

API at a glance

API What it does Result / important behavior
AtomicString.Get() Returns the current value (may be null). The current value.
AtomicString.Value Returns the current value (may be null). Returns the current value (may be null).
AtomicString.HasValue Returns true when a non-null value has been published. Returns true when a non-null value has been published.
AtomicString.Exchange(value) Atomically sets the value and returns the previous value (may be null). The value that was stored before the atomic update.
AtomicString.Clear() Clears the value (sets to null) and returns the previous value (may be null). The value that was stored before the atomic update.
AtomicString.TrySet(value) Attempts to set the value only if the current value is null. Returns true if the caller won the race and published value. true if the requested update was applied; otherwise, false.
AtomicString.TrySetIfNullOrEmpty(value) Attempts to set the value only if the current value is null or empty. Returns true if the value was set by this call. true if the requested update was applied; otherwise, false.
AtomicString.CompareExchange(value, comparand) Atomically replaces the value if the current value reference equals comparand. Returns the original value (may be null). The value observed before the compare-and-exchange attempt.
AtomicString.TryCompareExchange(value, comparand) Atomically replaces the value if the current value reference equals comparand. Returns true if the exchange occurred. true if atomically replaces the value if the current value reference equals . Returns true if the exchange occurred; otherwise, false.
AtomicString.GetOrAdd(factory) Returns the current value if present; otherwise computes a value, publishes it (best-effort), and returns the published value. The current value.
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Soenneker.Atomics.Strings:

Package Downloads
Soenneker.Utils.Environment

A utility library for useful environment related functionality

Soenneker.Utils.Paths.Resources

Resolves Resources directories across local, Azure, and GitHub Actions environments.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.19 37,472 8/29/2026
4.0.18 93 8/29/2026
4.0.17 85 8/29/2026
4.0.16 83,486 7/28/2026
4.0.15 32,137 7/16/2026
4.0.14 46,222 6/19/2026
4.0.13 45,252 6/5/2026
4.0.12 55,495 4/22/2026
4.0.10 48,720 3/12/2026
4.0.9 273 3/12/2026
4.0.8 120 3/12/2026
4.0.7 2,638 3/12/2026
4.0.6 13,886 3/10/2026
4.0.5 4,949 3/9/2026
4.0.4 130 3/9/2026
4.0.3 1,537 3/9/2026
4.0.2 14,497 3/4/2026
4.0.1 61,177 1/8/2026

Improve README documentation