Skip to content

iikuzmychov/BidirectionalDictionary

Repository files navigation

NuGet Downloads License

BidirectionalDictionary

Proper implementation of a bidirectional dictionary, also known as "BiMap" or "Two-way dictionary", for .NET Standard 2.0.

Example of usage

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"

Interfaces

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.

Read-only support

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);

License

The library is licensed under the MIT license.