2

how to add duplicate keys to dictionary

i.e i have already added the key,value pair as rollno,1 but i need to add the same parameter to the dictionary,but it is not allowing to add. how to add duplicated keys/repeated key in dictionary .

or any other choice.. any idea???

2
  • 2
    What's the point of using a dictionary for this ? Commented Jun 14, 2010 at 10:35
  • In case you are looking for alternatives of Dictionary without the LookUp feature, you can try named tuple as described in stackoverflow.com/questions/7745938/… Commented Oct 19, 2023 at 5:59

3 Answers 3

20

That doesn't make sense, if you added duplicate keys to the dictionary, how would it be able to find which one you want when you look them up?

Possibly you're looking for something like a List< KeyValuePair < T, T > >? Where you could store a list of pairs rather than an actual dictionary.

Sign up to request clarification or add additional context in comments.

Comments

5

Check out this:

What is the point of Lookup<TKey, TElement>?

You can use the Lookup class to help you create collections with duplicates keys.

Comments

0

I think it should work in your case

class Program
    {
         private static List<KeyValuePair<string, int>> d = new List<KeyValuePair<string, int>>();

        static void Main(string[] args)
        {
             d.Add(new KeyValuePair<string, int>("rollno", 1));
             d.Add(new KeyValuePair<string, int>("rollno", 2));
             d.Add(new KeyValuePair<string, int>("rollno", 3));
             var result = d.Where(x => x.Key == "joe");
            foreach(var q in result)
                Console.WriteLine(q.Value   );
            Console.ReadLine();
        }
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.