BiMap 2.0.10
dotnet add package BiMap --version 2.0.10
NuGet\Install-Package BiMap -Version 2.0.10
<PackageReference Include="BiMap" Version="2.0.10" />
<PackageVersion Include="BiMap" Version="2.0.10" />
<PackageReference Include="BiMap" />
paket add BiMap --version 2.0.10
#r "nuget: BiMap, 2.0.10"
#:package BiMap@2.0.10
#addin nuget:?package=BiMap&version=2.0.10
#tool nuget:?package=BiMap&version=2.0.10
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
ReaderWriterLockSlimfor high-concurrency read scenarios. - Atomic Operations:
Add,Remove, andClearoperations 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 | Versions 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. |
-
.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.
Corrected workflow name to match preferences.