ValueCollections 5.4.0
dotnet add package ValueCollections --version 5.4.0
NuGet\Install-Package ValueCollections -Version 5.4.0
<PackageReference Include="ValueCollections" Version="5.4.0" />
<PackageVersion Include="ValueCollections" Version="5.4.0" />
<PackageReference Include="ValueCollections" />
paket add ValueCollections --version 5.4.0
#r "nuget: ValueCollections, 5.4.0"
#:package ValueCollections@5.4.0
#addin nuget:?package=ValueCollections&version=5.4.0
#tool nuget:?package=ValueCollections&version=5.4.0
ValueCollections
A set of collections in C# implemented as struct to minimize heap allocations.
ValueList
An implementation of IList<T> using spans and array pools.
using ValueList<int> numbers = [];
for (int n = 0; n < 10; n++) {
numbers.Add(n);
}
using ValueList<int> evenNumbers = numbers.Where(number => number % 2 == 0);
Console.WriteLine(string.Join(", ", evenNumbers.ToList())); // 0, 2, 4, 6, 8
ValueList (Fixed Size)
A set of implementations of IList<T> using fixed-size inlined arrays.
Supported for capacities of [1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512].
ValueList128<int> numbers = [];
for (int n = 0; n < 10; n++) {
numbers.Add(n);
}
Console.WriteLine(string.Join(", ", numbers.Where(number => number % 2 == 0))); // 0, 2, 4, 6, 8
ValueHashSet
An implementation of ISet<T> using spans and array pools.
using ValueHashSet<int> numbers = [];
for (int n = 1; n <= 5; n++) {
numbers.Add(n);
}
for (int n = 3; n <= 7; n++) {
numbers.Add(n);
}
Console.WriteLine(string.Join(", ", numbers.ToArray())); // 1, 2, 3, 4, 5, 6, 7
ValueDictionary
An implementation of IDictionary<TKey, TValue> using spans and array pools.
using ValueDictionary<string, string> strings = [];
strings.Add("food", "pizza");
strings.Add("drink", "cola");
Console.WriteLine(string.Join(", ", strings.ToArray())); // [food, pizza], [drink, cola]
Benchmarks
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|---|---|---|---|---|---|
| SmallListOfStruct | 17.57 ns | 0.204 ns | 0.181 ns | 0.0255 | 80 B |
| SmallValueListOfStruct | 15.68 ns | 0.099 ns | 0.092 ns | - | - |
| SmallValueList32OfStruct | 13.75 ns | 0.054 ns | 0.047 ns | - | - |
| SmallListOfClass | 24.54 ns | 0.255 ns | 0.226 ns | 0.0306 | 96 B |
| SmallValueListOfClass | 20.17 ns | 0.077 ns | 0.072 ns | - | - |
| SmallValueList32OfClass | 23.62 ns | 0.133 ns | 0.118 ns | - | - |
| LargeListOfStruct | 20,617.90 ns | 169.942 ns | 150.649 ns | 41.6565 | 131400 B |
| LargeValueListOfStruct | 9,473.77 ns | 43.585 ns | 40.770 ns | - | - |
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated |
|---|---|---|---|---|---|---|---|
| SmallHashSetOfStruct | 92.44 ns | 1.886 ns | 2.097 ns | 0.1070 | - | - | 336 B |
| SmallValueHashSetOfStruct | 159.45 ns | 0.612 ns | 0.542 ns | - | - | - | - |
| SmallHashSetOfClass | 131.35 ns | 1.075 ns | 1.005 ns | 0.1173 | - | - | 368 B |
| SmallValueHashSetOfClass | 170.13 ns | 0.646 ns | 0.540 ns | - | - | - | - |
| LargeHashSetOfStruct | 178,224.06 ns | 958.507 ns | 800.397 ns | 95.2148 | 95.2148 | 95.2148 | 538656 B |
| LargeValueHashSetOfStruct | 227,838.63 ns | 1,594.403 ns | 1,413.397 ns | - | - | - | - |
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated |
|---|---|---|---|---|---|---|---|
| SmallDictionaryOfStructs | 101.2 ns | 1.04 ns | 0.98 ns | 0.1223 | - | - | 384 B |
| SmallValueDictionaryOfStructs | 114.8 ns | 0.26 ns | 0.24 ns | - | - | - | - |
| SmallDictionaryOfClasses | 152.4 ns | 1.27 ns | 1.18 ns | 0.1478 | - | - | 464 B |
| SmallValueDictionaryOfClasses | 203.0 ns | 0.89 ns | 0.79 ns | - | - | - | - |
| LargeDictionaryOfStructs | 229,800.1 ns | 3,274.30 ns | 2,902.59 ns | 124.7559 | 124.7559 | 124.7559 | 673106 B |
| LargeValueDictionaryOfStructs | 288,403.7 ns | 1,601.55 ns | 1,498.09 ns | - | - | - | - |
Gotchas
Value collections should be disposed after use, otherwise the internal rented array will not be returned to the pool.
using ValueList<string> strings = ["a", "b", "c"];
using ValueList<string> whitespaceStrings = strings.Where(string.IsNullOrWhiteSpace);
using ValueList<string> shortWhitespaceStrings = whitespaceStrings.Where(str => str.Length <= 10);
Special Thanks
- linkdotnet/StringBuilder for inspiration.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- 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.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.4.0 | 45 | 11/14/2025 |
| 5.3.0 | 317 | 6/12/2025 |
| 5.2.0 | 163 | 5/29/2025 |
| 5.1.0 | 141 | 5/22/2025 |
| 5.0.0 | 167 | 5/22/2025 |
| 4.2.0 | 152 | 5/8/2025 |
| 4.1.0 | 157 | 5/8/2025 |
| 4.0.0 | 174 | 5/8/2025 |
| 3.2.0 | 158 | 4/1/2025 |
| 3.1.0 | 170 | 3/31/2025 |
| 3.0.0 | 178 | 3/31/2025 |
| 2.1.0 | 463 | 3/26/2025 |
| 2.0.0 | 295 | 3/24/2025 |
| 1.3.0 | 278 | 3/23/2025 |
| 1.2.0 | 293 | 3/23/2025 |
| 1.1.0 | 261 | 3/23/2025 |
| 1.0.0 | 270 | 3/23/2025 |