Singulink.Collections 3.1.0

Prefix Reserved
dotnet add package Singulink.Collections --version 3.1.0                
NuGet\Install-Package Singulink.Collections -Version 3.1.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="Singulink.Collections" Version="3.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Singulink.Collections --version 3.1.0                
#r "nuget: Singulink.Collections, 3.1.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.
// Install Singulink.Collections as a Cake Addin
#addin nuget:?package=Singulink.Collections&version=3.1.0

// Install Singulink.Collections as a Cake Tool
#tool nuget:?package=Singulink.Collections&version=3.1.0                

Chat on Discord Build and Test

Library Package
Singulink.Collections View nuget packages
Singulink.Collections.Weak View nuget packages

Singulink.Collections provides generally useful collections that are missing from .NET. They are highly optimized for performance, well documented and follow the same design principles as built-in .NET collections so they should feel instantly familiar.

The following collections are included in the package:

  • HashSetDictionary: Collection of keys mapped to a hash set of unique values per key.
  • ListDictionary: Collection of keys mapped to a list of values per key.
  • Map: Collection of two types of values that map between each other in a bidirectional one-to-one relationship.
  • ReadOnlyHashSet: Fast read-only wrapper around a HashSet (instead of going through ISet<>).
  • ReadOnlyList: Fast read-only wrapper around a List (instead of going through IList<>).
  • A full set of interfaces and read-only wrappers for the new collections as well as an IReadOnlySet<> polyfill for .NET Standard.

Singulink.Collections.Weak provides a set of collection classes that store weak references to values so that the garbage collector is free to reclaim the memory they use when they aren't being referenced anymore. The values returned by the collections will never be null - if the value was garbage collected then the collection behaves as if the value was removed from the collection.

The following collections are included in the package:

  • WeakCollection: Collection of weakly referenced values that keeps items in an undefined order.
  • WeakList: Collection of weakly referenced values that maintains relative insertion order.
  • WeakValueDictionary: Collection of keys and weakly referenced values.

We are a small team of engineers and designers dedicated to building beautiful, functional and well-engineered software solutions. We offer very competitive rates as well as fixed-price contracts and welcome inquiries to discuss any custom development / project support needs you may have.

This package is part of our Singulink Libraries collection. Visit https://github.com/Singulink to see our full list of publicly available libraries and other open-source projects.

Installation

The packages are available on NuGet - simply install the Singulink.Collections and/or Singulink.Collections.Weak packages.

Supported Runtimes: Everywhere .NET Standard 2.0 is supported, including:

  • .NET
  • .NET Framework
  • Mono / Xamarin

End-of-life runtime versions that are no longer officially supported are not tested or supported by this library.

Usage

Map

// Create a map and optionally specify left and right value equality comparers
var numberToNameMap = new Map<int, string>(null, StringComparer.OrdinalIgnoreCase);

numberToNameMap.Add(1, "One");
numberToNameMap.Add(2, "Two");

numberToNameMap.ContainsLeft(2); // true

// Override values with indexer
numberToNameMap[2] = "Dos";
numberToNameMap.ContainsRight("Two"); // false, was overridden above

// Get a map with the reverse relationship
var nameToNumberMap = numberToNameMap.Reverse;

int one = nameToNumberMap["ONE"]; // 1

ListDictionary

Very similar API to HashSetDictionary, and both can be exposed as ICollectionDictionary:

var numberNames = new ListDictionary<int, string>();
numberNames[1].AddRange("One", "Uno");
numberNames[2].AddRange("Two", "Dos");

numberNames.ContainsValue("Two"); // true

// Empty lists are not part of the dictionary until a value is added

var threeNamesList = numberNames[3];
numberNames.ContainsKey(3); // false

threeNamesList.Add("Three");
numberNames.ContainsKey(3); // true

// Lists are automatically removed from the dictionary when they become empty

threeNamesList.Clear();
numberNames.TryGetValues(3, out threeNamesList); // false

// Examples of some of the supported ways to expose the dictionary through interfaces depending on your
// preferences and needs:

public class YourClass
{
    private ListDictionary<int, string> _numberNames;

    // Expose as Singulink IListDictionary (with .NET IList<string> values)
    public IListDictionary<int, string> NumberNames => _numberNames;

    // Expose as Singulink IReadOnlyListDictionary (with .NET IReadOnlyList<string> values)
    public IReadOnlyListDictionary<int, string> NumberNames => _numberNames.AsReadOnly();

    // Expose as Singulink ICollectionDictionary (with .NET ICollection<string> values)
    public ICollectionDictionary<int, string> NumberNames => _numberNames.AsCollectionDictionary();

    // Expose as Singulink IReadOnlyCollectionDictionary (with .NET IReadOnlyCollection<string> values)
    public IReadOnlyCollectionDictionary<int, string> NumberNames => _numberNames.AsReadOnlyCollectionDictionary();

    // Expose as .NET IReadOnlyDictionary<int, IList<string>>
    // Note that values can still be added/removed/modified through the value ILists even though it is
    // an IReadOnlyDictionary. Many of the additional API's present in the IDictionary interface are
    // not sensible in the context of a collection dictionary so that interface is not supported.
    public IReadOnlyDictionary<int, IList<string>> NumberNames => _numberNames;

    // Expose as .NET IReadOnlyDictionary<int, IReadOnlyList<string>> (fully read-only)
    public IReadOnlyDictionary<int, IReadOnlyList<string>> NumberNames => _numberNames.AsReadOnlyDictionaryOfList();

    // Expose as .NET IReadOnlyDictionary<int, ICollection<string>>
    public IReadOnlyDictionary<int, ICollection<string>> NumberNames => _numberNames.AsDictionaryOfCollection();

    // Expose as .NET IReadOnlyDictionary<int, IReadOnlyCollection<string>>
    public IReadOnlyDictionary<int, IReadOnlyCollection<string>> NumberNames => _numberNames.AsReadOnlyDictionaryOfCollection();
}

Further Reading

You can view the fully documented API on the project documentation site.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.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
3.1.0 135 3/23/2024
3.0.0 97 3/12/2024
2.0.0 188 7/26/2023
1.0.1 235 2/25/2023