BiMap 2.0.10

dotnet add package BiMap --version 2.0.10
                    
NuGet\Install-Package BiMap -Version 2.0.10
                    
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="BiMap" Version="2.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BiMap" Version="2.0.10" />
                    
Directory.Packages.props
<PackageReference Include="BiMap" />
                    
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 BiMap --version 2.0.10
                    
#r "nuget: BiMap, 2.0.10"
                    
#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.
#:package BiMap@2.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BiMap&version=2.0.10
                    
Install as a Cake Addin
#tool nuget:?package=BiMap&version=2.0.10
                    
Install as a Cake Tool

.NET NuGet NuGet Downloads License: MIT

BiMap

A thread-safe, high-performance, generic bidirectional map for .NET.

BiMap<TLeft, TRight> allows you to map keys to values and values back to keys with O(1) lookups in both directions. It ensures a strict 1-to-1 mapping between the two types.

Features

  • Bidirectional Mapping: Fast lookups from Left → Right and Right → Left.
  • Thread-Safe: Fully thread-safe implementation using ReaderWriterLockSlim for high-concurrency read scenarios.
  • Atomic Operations: Add, Remove, and Clear operations are atomic, ensuring both sides of the map remain synchronized.
  • .NET Standard 2.0: Compatible with .NET Framework 4.6.1+, .NET Core 2.0+, and all modern .NET versions.

Usage

Initialization

using BiMap;

// Create a new BiMap instance
var countryCodes = new BiMap<int, string>();

Adding Items

// Add unique pairs
// Note: Both Left and Right values must be unique in the collection.
bool added = countryCodes.Add(1, "US"); 
countryCodes.Add(44, "UK");
countryCodes.Add(90, "TR");

// Duplicate keys (on either side) will return false and not affect the map
bool result = countryCodes.Add(1, "Canada"); // Returns false

Retrieving Values

// Get Right from Left
string country = countryCodes.GetLeftToRight(90); // Returns "TR"
// Or use safe access
if (countryCodes.TryGetLeftToRight(44, out string name))
{
    Console.WriteLine(name); // "UK"
}

// Get Left from Right (Reverse lookup)
int code = countryCodes.GetRightToLeft("US"); // Returns 1
if (countryCodes.TryGetRightToLeft("UK", out int callingCode))
{
    Console.WriteLine(callingCode); // 44
}

Removing Items

Removing an item from one side automatically removes the corresponding mapping from the other side.

// Remove by Left key
countryCodes.RemoveByLeft(1); 
// Now "US" is also removed from the Right->Left index

// Remove by Right key
countryCodes.RemoveByRight("UK");
// Now 44 is also removed from the Left->Right index

Thread Safety

This library uses ReaderWriterLockSlim to strictly manage access:

  • Multiple readers can access the map simultaneously (e.g., Get, Contains, Enumerate).
  • Exclusive writers block all other readers and writers ensures data integrity (e.g., Add, Remove, Clear).
// Enumeration is thread-safe (returns a snapshot of the current state)
foreach (var pair in countryCodes.EnumerateLeftToRight())
{
    Console.WriteLine($"{pair.Key} -> {pair.Value}");
}

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.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 was computed. 
.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.

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
2.0.10 89 1/28/2026
2.0.9 86 1/28/2026
2.0.8 92 1/28/2026
2.0.7 85 1/28/2026
2.0.6 87 1/28/2026
2.0.5 82 1/28/2026
2.0.0 13,470 8/30/2021
1.0.0 815 8/6/2019

Corrected workflow name to match preferences.