Proper implementation of a bidirectional dictionary, also known as "BiMap" or "Two-way dictionary", for .NET Standard 2.0.
using System.Collections.Generic;
var countryCapitalDictionary = new BidirectionalDictionary<string, string>()
{
["Italy"] = "Rome",
["India"] = "New Delhi",
["USA"] = "Washington, D.C.",
};
Console.WriteLine(countryCapitalDictionary["Italy"]); // "Rome"
Console.WriteLine(countryCapitalDictionary.Inverse["Rome"]); // "Italy"
This library provides the following interfaces for greater flexibility:
IBidirectionalDictionary
IReadOnlyBidirectionalDictionary
Both BidirectionalDictionary
and ReadOnlyBidirectionalDictionary
implement these
interfaces, making it easier for you to work with various levels of abstraction.
If you need an read-only version of the bidirectional dictionary, the library provides an easy way to achieve this.
Use for BidirectionalDictionary
:
BidirectionalDictionary dictionary = ...;
var readOnlyDictionary = dictionary.AsReadOnly();
Use for IBidirectionalDictionary
:
using System.Collections.ObjectModel;
IBidirectionalDictionary dictionary = ...;
var readOnlyDictionary = new ReadOnlyBidirectionalDictionary<string, string>(dictionary);
The library is licensed under the MIT license.