ZeroAlloc.Analyzers
1.3.6
dotnet add package ZeroAlloc.Analyzers --version 1.3.6
NuGet\Install-Package ZeroAlloc.Analyzers -Version 1.3.6
<PackageReference Include="ZeroAlloc.Analyzers" Version="1.3.6"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="ZeroAlloc.Analyzers" Version="1.3.6" />
<PackageReference Include="ZeroAlloc.Analyzers"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ZeroAlloc.Analyzers --version 1.3.6
#r "nuget: ZeroAlloc.Analyzers, 1.3.6"
#:package ZeroAlloc.Analyzers@1.3.6
#addin nuget:?package=ZeroAlloc.Analyzers&version=1.3.6
#tool nuget:?package=ZeroAlloc.Analyzers&version=1.3.6
ZeroAlloc.Analyzers
Roslyn analyzers for modern .NET performance patterns. ZeroAlloc.Analyzers catches allocation-heavy patterns that built-in analyzers miss — FrozenDictionary opportunities, LINQ iterator overhead, boxing in loops, async state machine waste, and more — with 43 rules across 13 categories. Every rule is multi-TFM aware: rules that require a specific .NET version are automatically silenced when your project targets an older framework, so every diagnostic you see is actionable.
Installation
dotnet add package ZeroAlloc.Analyzers
Example
// warning ZA0105: Use TryGetValue instead of ContainsKey + indexer access.
// Before — two dictionary lookups
if (_cache.ContainsKey(key))
return _cache[key];
// After — single lookup, zero extra allocation
if (_cache.TryGetValue(key, out var value))
return value;
// warning ZA0201: Avoid string concatenation in loops; use StringBuilder or an interpolated string handler.
// Before
string result = "";
foreach (var item in items)
result += item + ", "; // allocates a new string every iteration
// After
var sb = new System.Text.StringBuilder();
foreach (var item in items)
sb.Append(item).Append(", ");
string result = sb.ToString();
Performance
Roslyn analyzers run incrementally; on a warmed-up build only changed files are re-analyzed, so the steady-state cost is proportional to the number of files you actually edit, not your whole codebase. TFM-gated rules that do not apply to your target framework register zero callbacks and add zero per-file overhead.
| Scenario | Rules active | Typical first-build overhead | Incremental overhead |
|---|---|---|---|
netstandard2.0 single-TFM |
29 of 43 | ~120 ms | ~10 ms |
net8.0 single-TFM |
43 of 43 | ~200 ms | ~15 ms |
net8.0 + netstandard2.0 multi-TFM |
43 / 29 per TFM | ~350 ms | ~25 ms |
net8.0, data-flow rules disabled (ZA0607, ZA0502) |
41 of 43 | ~160 ms | ~10 ms |
See docs/performance.md for tuning tips.
Features
- 43 rules across 13 categories: Collections, Strings, Memory, Logging, Boxing, LINQ, Regex, Enums, Sealing, Serialization, Async, Delegates, Value Types
- Multi-TFM aware — rules requiring net5.0+, net6.0+, net7.0+, or net8.0+ are automatically gated; you never see a diagnostic for an API that does not exist in your target
- Code fixes included for a subset of rules — apply suggestions with one click from the IDE or via
dotnet format - Zero transitive dependency — install with
PrivateAssets="all"so the package does not propagate to your consumers - IDE + CLI — diagnostics surface in Visual Studio, Rider, VS Code with C# Dev Kit, and
dotnet buildoutput
Documentation
| Page | Description |
|---|---|
| Getting Started | Install the package, understand first diagnostics, IDE setup, TFM awareness |
| Configuration | Severity tuning, suppression, TFM gating, TreatWarningsAsErrors |
| Collections (ZA01xx) | FrozenDictionary, FrozenSet, TryGetValue, pre-sizing, zero-length arrays |
| Strings (ZA02xx) | StringBuilder, AsSpan, string.Create, CompositeFormat, boxing in concatenation |
| Memory (ZA03xx) | stackalloc for small buffers, ArrayPool for large temporary arrays |
| Logging (ZA04xx) | LoggerMessage source generator vs reflection-based logging |
| Boxing (ZA05xx) | Value type boxing in loops, closure allocations, defensive copies |
| LINQ (ZA06xx) | LINQ in loops, Count vs Any, indexer vs First/Last, multiple enumeration |
| Regex (ZA07xx) | GeneratedRegex source generator vs runtime regex compilation |
| Enums (ZA08xx) | HasFlag boxing, Enum.ToString allocations, GetName/GetValues in loops |
| Sealing (ZA09xx) | Class sealing for JIT devirtualization |
| Serialization (ZA10xx) | JSON source generation vs reflection-based serialization |
| Async (ZA11xx) | Elide async/await on tail calls, dispose CancellationTokenSource, Span in async |
| Delegates (ZA14xx) | Static lambda caching, closure elimination |
| Value Types (ZA15xx) | Struct GetHashCode override, avoid finalizers |
| Build Performance | Analyzer build-time overhead, TFM gating, CI vs local configuration |
| Testing with Analyzers | Suppress warnings in tests, write Roslyn diagnostic tests, TFM-gated rule testing |
License
MIT
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.