Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 73409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:11:49+00:00 2026-05-10T20:11:49+00:00

I am looking for a generic, bidirectional 1 to 1 Dictionary class in C#

  • 0

I am looking for a generic, bidirectional 1 to 1 Dictionary class in C# (2), ie. a BiDictionaryOneToOne<T, S> which is guaranteed to only contain one of each value and key (up to RefEquals anyway), and which can be searched using either key or value. Anyone know of one, or should I just implement it myself? I can’t believe that I’m the first person to need this…

There is a BiDictionary in the answers to this question, but it is not for unique elements (and also does not implement RemoveByFirst(T t) or RemoveBySecond(S s)).

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:11:50+00:00Added an answer on May 10, 2026 at 8:11 pm

    OK, here is my attempt (building on Jon’s – thanks), archived here and open for improvement :

    /// <summary> /// This is a dictionary guaranteed to have only one of each value and key.  /// It may be searched either by TFirst or by TSecond, giving a unique answer because it is 1 to 1. /// </summary> /// <typeparam name='TFirst'>The type of the 'key'</typeparam> /// <typeparam name='TSecond'>The type of the 'value'</typeparam> public class BiDictionaryOneToOne<TFirst, TSecond> {     IDictionary<TFirst, TSecond> firstToSecond = new Dictionary<TFirst, TSecond>();     IDictionary<TSecond, TFirst> secondToFirst = new Dictionary<TSecond, TFirst>();      #region Exception throwing methods      /// <summary>     /// Tries to add the pair to the dictionary.     /// Throws an exception if either element is already in the dictionary     /// </summary>     /// <param name='first'></param>     /// <param name='second'></param>     public void Add(TFirst first, TSecond second)     {         if (firstToSecond.ContainsKey(first) || secondToFirst.ContainsKey(second))             throw new ArgumentException('Duplicate first or second');          firstToSecond.Add(first, second);         secondToFirst.Add(second, first);     }      /// <summary>     /// Find the TSecond corresponding to the TFirst first     /// Throws an exception if first is not in the dictionary.     /// </summary>     /// <param name='first'>the key to search for</param>     /// <returns>the value corresponding to first</returns>     public TSecond GetByFirst(TFirst first)     {         TSecond second;         if (!firstToSecond.TryGetValue(first, out second))             throw new ArgumentException('first');          return second;      }      /// <summary>     /// Find the TFirst corresponing to the Second second.     /// Throws an exception if second is not in the dictionary.     /// </summary>     /// <param name='second'>the key to search for</param>     /// <returns>the value corresponding to second</returns>     public TFirst GetBySecond(TSecond second)     {         TFirst first;         if (!secondToFirst.TryGetValue(second, out first))             throw new ArgumentException('second');          return first;      }       /// <summary>     /// Remove the record containing first.     /// If first is not in the dictionary, throws an Exception.     /// </summary>     /// <param name='first'>the key of the record to delete</param>     public void RemoveByFirst(TFirst first)     {         TSecond second;         if (!firstToSecond.TryGetValue(first, out second))             throw new ArgumentException('first');          firstToSecond.Remove(first);         secondToFirst.Remove(second);     }      /// <summary>     /// Remove the record containing second.     /// If second is not in the dictionary, throws an Exception.     /// </summary>     /// <param name='second'>the key of the record to delete</param>     public void RemoveBySecond(TSecond second)     {         TFirst first;         if (!secondToFirst.TryGetValue(second, out first))             throw new ArgumentException('second');          secondToFirst.Remove(second);         firstToSecond.Remove(first);     }      #endregion      #region Try methods      /// <summary>     /// Tries to add the pair to the dictionary.     /// Returns false if either element is already in the dictionary             /// </summary>     /// <param name='first'></param>     /// <param name='second'></param>     /// <returns>true if successfully added, false if either element are already in the dictionary</returns>     public Boolean TryAdd(TFirst first, TSecond second)     {         if (firstToSecond.ContainsKey(first) || secondToFirst.ContainsKey(second))             return false;          firstToSecond.Add(first, second);         secondToFirst.Add(second, first);         return true;     }       /// <summary>     /// Find the TSecond corresponding to the TFirst first.     /// Returns false if first is not in the dictionary.     /// </summary>     /// <param name='first'>the key to search for</param>     /// <param name='second'>the corresponding value</param>     /// <returns>true if first is in the dictionary, false otherwise</returns>     public Boolean TryGetByFirst(TFirst first, out TSecond second)     {         return firstToSecond.TryGetValue(first, out second);     }      /// <summary>     /// Find the TFirst corresponding to the TSecond second.     /// Returns false if second is not in the dictionary.     /// </summary>     /// <param name='second'>the key to search for</param>     /// <param name='first'>the corresponding value</param>     /// <returns>true if second is in the dictionary, false otherwise</returns>     public Boolean TryGetBySecond(TSecond second, out TFirst first)     {         return secondToFirst.TryGetValue(second, out first);     }      /// <summary>     /// Remove the record containing first, if there is one.     /// </summary>     /// <param name='first'></param>     /// <returns> If first is not in the dictionary, returns false, otherwise true</returns>     public Boolean TryRemoveByFirst(TFirst first)     {         TSecond second;         if (!firstToSecond.TryGetValue(first, out second))             return false;          firstToSecond.Remove(first);         secondToFirst.Remove(second);         return true;     }      /// <summary>     /// Remove the record containing second, if there is one.     /// </summary>     /// <param name='second'></param>     /// <returns> If second is not in the dictionary, returns false, otherwise true</returns>     public Boolean TryRemoveBySecond(TSecond second)     {         TFirst first;         if (!secondToFirst.TryGetValue(second, out first))             return false;          secondToFirst.Remove(second);         firstToSecond.Remove(first);         return true;     }      #endregion              /// <summary>     /// The number of pairs stored in the dictionary     /// </summary>     public Int32 Count     {         get { return firstToSecond.Count; }     }      /// <summary>     /// Removes all items from the dictionary.     /// </summary>     public void Clear()     {         firstToSecond.Clear();         secondToFirst.Clear();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I think the problem here is because jQuery doesn't set… May 11, 2026 at 8:40 am
  • added an answer From The C# Team's answers to frequently asked questions: When… May 11, 2026 at 8:40 am
  • added an answer That code is for Macintosh Common Lisp (MCL). It will… May 11, 2026 at 8:40 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.