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

  • SEARCH
  • Home
  • 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 64027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:43:32+00:00 2026-05-10T18:43:32+00:00

It’s easy to get the value of a key from a .NET generic Dictionary:

  • 0

It’s easy to get the value of a key from a .NET generic Dictionary:

Dictionary<int, string> greek = new Dictionary<int, string>(); greek.Add(1, 'Alpha'); greek.Add(2, 'Beta'); string secondGreek = greek[2];  // Beta 

But trying to get the keys given a value is not as straightforward because there could be multiple keys:

int[] betaKeys = greek.WhatDoIPutHere('Beta');  // expecting single 2 
  • 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-10T18:43:32+00:00Added an answer on May 10, 2026 at 6:43 pm

    Okay, here’s the multiple bidirectional version:

    using System; using System.Collections.Generic; using System.Text;  class BiDictionary<TFirst, TSecond> {     IDictionary<TFirst, IList<TSecond>> firstToSecond = new Dictionary<TFirst, IList<TSecond>>();     IDictionary<TSecond, IList<TFirst>> secondToFirst = new Dictionary<TSecond, IList<TFirst>>();      private static IList<TFirst> EmptyFirstList = new TFirst[0];     private static IList<TSecond> EmptySecondList = new TSecond[0];      public void Add(TFirst first, TSecond second)     {         IList<TFirst> firsts;         IList<TSecond> seconds;         if (!firstToSecond.TryGetValue(first, out seconds))         {             seconds = new List<TSecond>();             firstToSecond[first] = seconds;         }         if (!secondToFirst.TryGetValue(second, out firsts))         {             firsts = new List<TFirst>();             secondToFirst[second] = firsts;         }         seconds.Add(second);         firsts.Add(first);     }      // Note potential ambiguity using indexers (e.g. mapping from int to int)     // Hence the methods as well...     public IList<TSecond> this[TFirst first]     {         get { return GetByFirst(first); }     }      public IList<TFirst> this[TSecond second]     {         get { return GetBySecond(second); }     }      public IList<TSecond> GetByFirst(TFirst first)     {         IList<TSecond> list;         if (!firstToSecond.TryGetValue(first, out list))         {             return EmptySecondList;         }         return new List<TSecond>(list); // Create a copy for sanity     }      public IList<TFirst> GetBySecond(TSecond second)     {         IList<TFirst> list;         if (!secondToFirst.TryGetValue(second, out list))         {             return EmptyFirstList;         }         return new List<TFirst>(list); // Create a copy for sanity     } }  class Test {     static void Main()     {         BiDictionary<int, string> greek = new BiDictionary<int, string>();         greek.Add(1, 'Alpha');         greek.Add(2, 'Beta');         greek.Add(5, 'Beta');         ShowEntries(greek, 'Alpha');         ShowEntries(greek, 'Beta');         ShowEntries(greek, 'Gamma');     }      static void ShowEntries(BiDictionary<int, string> dict, string key)     {         IList<int> values = dict[key];         StringBuilder builder = new StringBuilder();         foreach (int value in values)         {             if (builder.Length != 0)             {                 builder.Append(', ');             }             builder.Append(value);         }         Console.WriteLine('{0}: [{1}]', key, builder);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.