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 542699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:28:27+00:00 2026-05-13T10:28:27+00:00

I declared the dictionary obj. Dictionary<string, string> aDict = new Dictionary<string, string>(); aDict .Add(IP,

  • 0

I declared the dictionary obj.

Dictionary<string, string> aDict = new Dictionary<string, string>(); 
        aDict .Add("IP", "Host"); 

As I remembered,

The expression of aDict[IP] can return the value (Host).

Now if I go in the opposite direction.

How to get the Key from Value ? aDict[Host] ?

Does the Dictionary is a one-way street in C#, only running from Key to Value ? 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. Editorial Team
    Editorial Team
    2026-05-13T10:28:27+00:00Added an answer on May 13, 2026 at 10:28 am

    Dictionaries do not work like this, nor are they intended to. How would you resolve the following:

    key = "1", value = "x",
    key = "2", value = "x"
    

    You could do this:

    var keys = dict.Where(kvp => kvp.Value == someValue).Select(kvp => kvp.Key);
    foreach(var key in keys) {
        Console.WriteLine(key);
    }
    

    But if you really need to go back and forth between keys and values you should consider encapsulating the problem into a two-way map. Here’s a very simple implementation of this that you can tailor to your needs:

    class TwoWayDictionary<TLeft, TRight> {
        IDictionary<TLeft, TRight> leftToRight = new Dictionary<TLeft, TRight>();
        IDictionary<TRight, TLeft> rightToLeft = new Dictionary<TRight, TLeft>();
    
        public void Add(TLeft left, TRight right) {
            if (leftToRight.ContainsKey(left)) {
                throw new InvalidOperationException("key left is duplicate");
            }
            if (rightToLeft.ContainsKey(right)) {
                throw new InvalidOperationException("key right is duplicate");
            }
            leftToRight.Add(left, right);
            rightToLeft.Add(right, left);
        }
    
        public bool TryGetRightByLeft(TLeft left, out TRight right) {
            return leftToRight.TryGetValue(left, out right);
        }
    
        public bool TryGetLeftByRight(out TLeft left, TRight right) {
            return rightToLeft.TryGetValue(right, out left);
        }
    }
    

    Note that this assumes that no key is ever duplicated.

    Now you can say:

    TwoWayDictionary<string, string> dict = new TwoWayDictionary<string, string>();
    dict.Add("127.0.0.1", "localhost");
    
    string host;
    dict.TryGetRightByLeft("127.0.0.1", out host);
    // host is "localhost"
    
    string ip;
    dict.TryGetLeftByRight("localhost", out ip);
    // ip is "127.0.0.1"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • 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
  • Editorial Team
    Editorial Team added an answer .com was captured by the last part of the regex… May 16, 2026 at 10:33 am
  • Editorial Team
    Editorial Team added an answer https://learn.microsoft.com/en-us/archive/blogs/ericlippert/the-stack-is-an-implementation-detail-part-one The whole "reference types on the heap, value types… May 16, 2026 at 10:33 am
  • Editorial Team
    Editorial Team added an answer The best option, if possible in your application, is cooperative… May 16, 2026 at 10:33 am

Trending Tags

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

Top Members

Related Questions

If I declared a dictionary like this: private static Dictionary<string, object> aDict = new
I declared a static mainDict like this, private static Dictionary<string, object> mainDict = new
As I know if i declared a dictionary, i could call myDict.Clear() for reusing
I have the following dictionary declared: private readonly Dictionary<int, Image> dictionary; And I have
I've the following doubt. I've a page MyPage and i've declared few dictionary objects
What I mean is, can a variable/array declared and initialized be used in HTML,
The parameters dictionary contains a null entry for parameter 'appId' of non-nullable type 'System.Int32'
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I've declared a field in my INNODB/MySQL table as VARCHAR(255) CHARACTER SET utf8 NOT
What is the difference between a variable declared as dynamic and a variable declared

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.