ValueCollections 5.2.0

dotnet add package ValueCollections --version 5.2.0
                    
NuGet\Install-Package ValueCollections -Version 5.2.0
                    
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="ValueCollections" Version="5.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ValueCollections" Version="5.2.0" />
                    
Directory.Packages.props
<PackageReference Include="ValueCollections" />
                    
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 ValueCollections --version 5.2.0
                    
#r "nuget: ValueCollections, 5.2.0"
                    
#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.
#addin nuget:?package=ValueCollections&version=5.2.0
                    
Install ValueCollections as a Cake Addin
#tool nuget:?package=ValueCollections&version=5.2.0
                    
Install ValueCollections as a Cake Tool

ValueCollections

NuGet

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

Product 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 was computed.  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.
  • 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.2.0 118 5/29/2025
5.1.0 116 5/22/2025
5.0.0 123 5/22/2025
4.2.0 120 5/8/2025
4.1.0 117 5/8/2025
4.0.0 113 5/8/2025
3.2.0 132 4/1/2025
3.1.0 125 3/31/2025
3.0.0 127 3/31/2025
2.1.0 440 3/26/2025
2.0.0 249 3/24/2025
1.3.0 244 3/23/2025
1.2.0 252 3/23/2025
1.1.0 239 3/23/2025
1.0.0 245 3/23/2025