0

I have a Dictionary<int, int> in my class. How can I access both values without knowing the key?

For instance, I want to be able to do something like this: If the dictionary contains Dictionary<int, int>
and the values are <5, 4>
I want to be able to get the value of <(this),(this)> like

Pseudo code:

foreach(Dictionary item or row)
{
    my first int = Dictionary<(this), (not this)>
    my second int = Dictionary<(not this), (this)>
}

How can I do this using a dictionary? If this is not doable: Is there another way?

dtb
  • 213,145
  • 36
  • 401
  • 431
user710502
  • 11,181
  • 29
  • 106
  • 161
  • Sounds like `Dictionary` is not the right abstraction for your use case. – K-ballo Sep 25 '11 at 04:16
  • I need a good suggestion then :) – user710502 Sep 25 '11 at 04:17
  • Can you elaborate more? I read 5 times and still don't know what you want... – Adriano Carneiro Sep 25 '11 at 04:20
  • Sure... Basically I created a Dictionary to contain two ints.. the problem is that I think the first item is the key and I would really want to be able to retrieve both values like I explained above ... I should be able to get the first "int" if I wanted to.. or the "second" just like an array.. I am new to c# so bare with me – user710502 Sep 25 '11 at 04:24
  • Perhaps if you explained more about the data your access (real world) and the access patterns. I don't get the (not this} and I don't understand why you're storing a dictionary of dicationaries – bryanmac Sep 25 '11 at 04:25
  • Are you sure you don't want a List to get to the ints? A dictionary is a lookup table - like the index in a book. Its used to look up a value by a key. – bryanmac Sep 25 '11 at 04:27
  • I am not storing Dictionary of Dictionaries but someone that provided the answer below understood my issue.. thanks – user710502 Sep 25 '11 at 04:28
  • If i were to use a List, I would needed to be List is this possible? – user710502 Sep 25 '11 at 04:29
  • possible duplicate of [What is the best way to iterate over a Dictionary in C#?](http://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c) – Rick Sladkey Sep 25 '11 at 04:34
  • You could do a List, where MyObject has 2 int properties (or use a List>). Dictionary indicates that the first int (the key) is unique in the collection. If you could have, say, <1,2> and <1,3> in your collection you should not use a dictionary. – Mathias Sep 26 '11 at 05:51

1 Answers1

2
foreach (KeyValuePair<int, int> kvp in myDictionary)
{
   var first = kvp.Key;
   var second = kvp.Value;
}
Mathias
  • 15,191
  • 9
  • 60
  • 92