DictionaryEntry brings Rust's entry() API to C# Dictionary<TKey, TValue>, replacing multi-line TryGetValue + indexer boilerplate with expressive, single-lookup operations.
By avoiding repeated hashing and probing on inserts and updates, DictionaryEntry runs up to ~2x as fast as hand-written TryGetValue + indexer code in the miss and update benchmarks below, with zero-allocation APIs, NativeAOT compatibility, ReadOnlySpan<char> lookups, and zero-boxing C# 15 [Union] pattern matching.
// Increment or initialize in 1 lookup (instead of TryGetValue + dict[key] = ...)
scores.Entry(player).OrDefault() += points;
// Lazily initialize a collection and mutate it in place
groups.Entry(category).OrInsertWith(static () => []).Add(item);
// Zero-allocation ReadOnlySpan<char> lookup on Dictionary<string, int>
counts.Entry(line.AsSpan(0, 5)).AndModify(static c => c + 1).OrInsert(1);
// Exhaustive, zero-boxing C# 15 union switch
string label = users.Entry(userId) switch
{
OccupiedEntry<Guid, User> occupied => occupied.Value.Name,
VacantEntry<Guid, User> => "Guest",
};- Installation
- Common Use Cases
- Benchmarks
- Advanced Features
- Performance Considerations
- Limitations and Caveats
- License
dotnet add package DictionaryEntryTargets net9.0, net10.0, and net11.0 (IsAotCompatible = true).
Standard C# upserts hash and probe the dictionary twice—once in TryGetValue and again in the indexer setter:
// Traditional C# (2 lookups, 9 lines)
if (scores.TryGetValue(player, out var score))
{
scores[player] = score + points;
}
else
{
scores[player] = points;
}
// DictionaryEntry: live ref return (1 lookup, 0 allocations)
scores.Entry(player).OrDefault() += points;
// DictionaryEntry: distinct update vs. initial value (1 lookup, 0 allocations)
scores.Entry(player)
.AndModify(points, static (score, pts) => score + pts)
.OrInsert(initialScore);OrInsert, OrInsertWith, and OrInsertWithKey return a live ref TValue into the dictionary, evaluating factory delegates only when the key is absent:
// Simple default value
int port = config.Entry("port").OrInsert(8080);
// Lazy factory (only invoked when missing) + immediate mutation
buckets.Entry(routeKey)
.OrInsertWith(static () => new List<Request>())
.Add(request);
// Key-aware factory with state (0 closure allocations)
int scaled = cache.Entry(key)
.OrInsertWithKey(multiplier, static (k, mult) => Compute(k, mult));Mutating a value-type entry in a standard Dictionary<TKey, TStruct> copies the struct out, modifies the copy, and hashes the key a second time to write it back. DictionaryEntry mutates the slot directly in place with zero struct copies:
// Upsert + in-place struct field mutation via RefAction
playerStats.Entry(playerId)
.AndModify(damage, static (ref stats, dmg) =>
{
stats.HitsLanded++;
stats.TotalDamage += dmg;
})
.OrInsert(new PlayerStats { HitsLanded = 1, TotalDamage = damage });
// Update in place ONLY if the key already exists
if (playerStats.Entry(playerId) is { IsOccupied: true } entry)
{
ref var stats = ref entry.ValueRef;
stats.HitsLanded++;
stats.TotalDamage += damage;
}For Dictionary<string, TValue>, pass a ReadOnlySpan<char> (or Span<char> / char[]) directly to .Entry(span). Cache hits allocate 0 bytes, and cache misses materialize the string key only once when inserting:
var headers = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
ReadOnlySpan<char> line = "Content-Length: 512";
ReadOnlySpan<char> name = line[..line.IndexOf(':')];
headers.Entry(name).OrDefault()++;Benchmarked on November 17, 2026, at source revision af9e3b9 with BenchmarkDotNet v0.15.8 on .NET 11.0.0 (Linux NixOS 26.11, Intel Core i9-14900KF, x64 RyuJIT). Comparing idiomatic hand-written TryGetValue + indexer code against DictionaryEntry:
| Scenario | Key / Value | Hand-Written | DictionaryEntry |
vs. Hand-Written | Allocated |
|---|---|---|---|---|---|
| Get-or-Add (Hit) | int, int |
1.244 ns |
1.360 ns |
~9% slower | 0 B |
| Get-or-Add (Miss) | int, int |
3.473 ns |
2.700 ns |
1.29x faster | 0 B |
| Get-or-Default (Hit) | string, int |
2.968 ns |
3.254 ns |
~10% slower | 0 B |
| Get-or-Default (Miss) | string, string? |
7.203 ns |
4.875 ns |
1.48x faster | 0 B |
| Stateful Factory (Miss) | string, int |
8.095 ns |
6.664 ns |
1.21x faster | 0 B |
Update Existing via ValueRef (Hit) |
string, int |
5.899 ns |
2.966 ns |
1.99x faster | 0 B |
Increment Counter via AndModify (Hit) |
string, int |
5.904 ns |
3.674 ns |
1.61x faster | 0 B |
Increment Counter via AndModify (Miss) |
string, int |
7.694 ns |
5.834 ns |
1.32x faster | 0 B |
Union switch Upsert (Hit) |
string, int |
6.654 ns |
3.558 ns |
1.87x faster | 0 B |
Union switch Upsert (Miss) |
string, int |
7.556 ns |
6.217 ns |
1.22x faster | 0 B |
| Direct Span Lookup (Hit) | ReadOnlySpan<char> |
19.914 ns (ToString) |
6.567 ns |
3.03x faster | 0 B vs 53 B |
Results depend on the workload and machine; the full suite also includes scenarios where built-in dictionary operations are faster.
Why is DictionaryEntry faster on misses and mutations?
- Single key hash & bucket probe:
TryGetValuefollowed bydict[key] = valuehashes the key and walks the bucket chain twice.Entrycaches the slot reference on hits and the computed hash code on misses. - O(1)
OccupiedEntry.Insert:OccupiedEntryrecords the entry's byte offset and a structural version stamp, writing updated values directly to the slot without re-probing. - Zero-boxing structs:
Entry<TKey, TValue>is a stack-onlyref structimplementing C# 15's non-boxingTryGetValueunion pattern rather thanobject-backed storage.
For the full benchmark suite and instructions to run them locally, see src/DictionaryEntry.Benchmarks/README.md.
Entry<TKey, TValue> is decorated with [Union] over OccupiedEntry<TKey, TValue> and VacantEntry<TKey, TValue> (and implements System.Runtime.CompilerServices.IUnion on .NET 11+), enabling compiler-verified exhaustive switch expressions, switch statements, and is patterns without boxing:
// Exhaustive switch expression (no '_' discard arm needed)
string tier = scores.Entry(player) switch
{
OccupiedEntry<string, int> { Value: >= 1000 } => "Gold",
OccupiedEntry<string, int>(_, >= 100) => "Silver", // positional Deconstruct(out key, out value)
OccupiedEntry<string, int> => "Bronze",
VacantEntry<string, int> => "Unranked",
};
// 'is' pattern matching (Rust 'if let' style)
if (users.Entry(userId) is OccupiedEntry<Guid, User> occupied)
{
Console.WriteLine($"Found {occupied.Key}: {occupied.Value.Name}");
}
// Switch statement with local variables (no delegates or closures)
switch (users.Entry(userId))
{
case OccupiedEntry<Guid, User> existing:
existing.Insert(existing.Value with { LastSeen = now });
break;
case VacantEntry<Guid, User> vacant:
vacant.Insert(CreateNewUser(vacant.Key, now));
break;
}On C# 13 / 14—or when you prefer generic type inference over spelling out <TKey, TValue> in case patterns—use Match, TryGetOccupied, and TryGetVacant:
string status = users.Entry(userId).Match(
occupied => $"Found: {occupied.Value.Name}",
vacant => $"Created: {vacant.Insert(CreateNewUser(vacant.Key)).Name}"
);
if (users.Entry(userId).TryGetOccupied(out var occupied))
{
// Remove and return the value in a single flow
User removed = occupied.Remove();
}While dict.Entry(ReadOnlySpan<char>) is convenient for one-off lookups on Dictionary<string, TValue>, tight loops and custom alternate key types (such as UTF-8 ReadOnlySpan<byte> via IAlternateEqualityComparer<ReadOnlySpan<byte>, string>) can hoist GetAlternateLookup<TAlternateKey>() once outside the loop:
var counts = new Dictionary<string, int>(StringComparer.Ordinal);
var lookup = counts.GetAlternateLookup<ReadOnlySpan<char>>();
ReadOnlySpan<char> csv = "apple,banana,apple,cherry";
foreach (var range in csv.Split(','))
{
lookup.Entry(csv[range]).AndModify(static c => c + 1).OrInsert(1);
}
// Remove via alternate key and recover both the stored string key and value
if (lookup.Entry("banana".AsSpan()).TryGetOccupied(out var banana))
{
(string storedKey, int finalCount) = banana.RemoveEntry();
}| Type | Kind | Key Members |
|---|---|---|
Entry<TKey, TValue> |
ref struct ([Union]) |
Key, IsOccupied, IsVacant, ValueRef, OrInsert(v), OrDefault(), OrInsertWith(...), OrInsertWithKey(...), AndModify(...), InsertEntry(v), ToOccupied(), ToVacant(), TryGetOccupied(out ...), TryGetVacant(out ...), Match(...) |
OccupiedEntry<TKey, TValue> |
readonly struct |
Key, Value (snapshot), Deconstruct(out key, out value), Insert(newValue), Remove(), RemoveEntry() |
VacantEntry<TKey, TValue> |
readonly struct |
Key, Insert(value), InsertEntry(value) |
AlternateEntry<TKey, TValue, TAlt> |
ref struct |
Same fluent surface as Entry<TKey, TValue>, keyed by TAlt (allows ref struct) |
AlternateOccupiedEntry<...> |
readonly ref struct |
Key, Value (snapshot), Deconstruct, Insert(newValue), Remove(), RemoveEntry() (returns (TKey, TValue)) |
AlternateVacantEntry<...> |
readonly ref struct |
Key, Insert(value), InsertEntry(value) |
Methods taking delegates (AndModify, OrInsertWith, OrInsertWithKey, and Match) are zero-allocation when passed a static lambda or non-capturing lambda, because C# caches a singleton delegate instance.
If a lambda captures local variables or this, C# allocates a closure object (24–88 B) even on cache hits before the method runs:
// Avoid on hot paths: captures 'amount', allocating a closure on every call
scores.Entry(key).AndModify(c => c + amount).OrInsert(amount);
// Zero-allocation Option 1: TState overload + static lambda (also supports ref struct / Span state)
scores.Entry(key).AndModify(amount, static (c, amt) => c + amt).OrInsert(amount);
// Zero-allocation Option 2: ref return
scores.Entry(key).OrDefault() += amount;
// Zero-allocation Option 3: C# 15 union switch
switch (scores.Entry(key))
{
case OccupiedEntry<string, int> occupied:
occupied.Insert(occupied.Value + amount);
break;
case VacantEntry<string, int> vacant:
vacant.Insert(amount);
break;
}Entry<TKey, TValue> holds a managed ref TValue into the dictionary, so Entry itself cannot be boxed, stored in heap fields, or held across await boundaries.
However, OccupiedEntry<TKey, TValue> and VacantEntry<TKey, TValue> are regular readonly structs (not ref structs). Unwrapping Entry in a C# 15 switch before hitting await works seamlessly:
// Works on C# 15: Entry is unwrapped into OccupiedEntry / VacantEntry before 'await'
var user = cache.Entry(userId) switch
{
OccupiedEntry<Guid, User> occupied => occupied.Value,
VacantEntry<Guid, User> vacant => vacant.Insert(await db.LoadUserAsync(vacant.Key)),
};
// Works on C# 13 / 14: return a Task from Match and await it
var user = await cache.Entry(userId).Match(
occupied => Task.FromResult(occupied.Value),
async vacant => vacant.Insert(await db.LoadUserAsync(vacant.Key))
);(Note: AlternateOccupiedEntry and AlternateVacantEntry hold TAlternateKey—such as ReadOnlySpan<char>—so all three Alternate* types are ref structs, cannot cross await boundaries, and cannot participate in C# 15 [Union] type patterns (CS8121).)
- Compiler version: Native union
switch/ispatterns require compiling with C# 15 (.NET 11SDK; projects targetingnet9.0ornet10.0on the.NET 11SDK can opt in with<LangVersion>15.0</LangVersion>or<LangVersion>latest</LangVersion>). All fluent methods,Match, andTryGet*work on earlier C# versions. - Explicit type arguments in case patterns: C# 15 unions are type unions rather than nominal enum variants, so
<TKey, TValue>must be written inOccupiedEntry<TKey, TValue>/VacantEntry<TKey, TValue>type patterns (or aliased withusing). - Live
Entryvs. snapshot cases:OccupiedEntry.Valueis a snapshot captured when the entry was obtained and is not updated byInsert. Converting anOccupiedEntryorVacantEntryback toEntry<TKey, TValue>performs a fresh lookup against the dictionary's current state.
- Do not mutate the dictionary externally while holding an
Entry: BecauseEntryandAlternateEntrycache an interior value reference into the dictionary's storage, mutating or resizing the sameDictionary<TKey, TValue>outside the entry's own methods (including inside anAndModifycallback) invalidates that entry instance. - Runtime internals:
DictionaryEntryuses[UnsafeAccessor]to avoid rehashing keys on vacant insertions and to perform O(1) slot writes onOccupiedEntry.Insert. It is verified on.NET 9,.NET 10, and.NET 11; newer.NETreleases may require an updated package version ifDictionary<TKey, TValue>'s internal layout changes. - Thread safety:
Dictionary<TKey, TValue>is not thread-safe. Synchronize concurrent access externally or use a concurrent collection.
DictionaryEntry is MIT Licensed.