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"

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:

var readOnlyDictionary = dictionary.AsReadOnly();

Use for IBidirectionalDictionary:

using System.Collections.ObjectModel;

var readOnlyDictionary = new ReadOnlyBidirectionalDictionary<string, string>(dictionary);

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 abstractions.

License

The library is licensed under the MIT license.