0

Assumed I defined my dict as below.

Dictionary<list<int>, list<string>> d = new Dictionary<List<int>, list<string>>()

How can I retrieve the dict key and value rapidly.

[update]

I want to get the Key - List content. I tried a simple way as below

List<int> KeysList= new List<int>(d.Keys);

But it doesn't work at the complex key on my case.

Can I only use the KeyValuePair as below?

foreach (KeyValuePair<List<int>, List<string>> pair in d) { KeysList= new List<int>(pair.Key); } 
5
  • 1
    What do you mean by rapidly? And what are you trying to access? Commented Aug 19, 2010 at 8:10
  • @erash. I updated my rapidly way. like the simple way of d.keys Commented Aug 19, 2010 at 8:22
  • Is it possible you're looking for Dictionary<int, string>? That'd make the last line List<int> KeysList = d.Keys;. Commented Aug 19, 2010 at 8:24
  • @Kobi, No. Actually I am looking for List<int> as the KeysList (assumed there are one Key collected some items of List<int> and one value collected some items of List<string>) Commented Aug 19, 2010 at 8:28
  • Oh, Ok. Can you please add example of such a dictionary (e.g. Keys are {1}, {1,2,3}, {20, 30, 1, 1}), and an example of the output you're expecting? Commented Aug 19, 2010 at 8:34

1 Answer 1

3

You've got a potential problem there to start with: List<T> doesn't override Equals, so you'd have to use the exact same key reference to fetch the value for a key.

Having said that, if you have that reference, it's as simple as

List<string> value = d[key]; 

or

List<string> value; if (d.TryGetValue(key, out value)) { ... } 

It's pretty unusual to have a List<T> as a key though - can you tell us more about what you're trying to do? There may be a better approach.

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

3 Comments

Can you please elaborate on the first point - what do you mean by "exact same key reference"? Wouldn't it work with two references to the same list, or with references from Dictionary.Keys?
I can't try List<string> value = d[key];. It doesn't work on my case. It's a function call, I don't know the Key exactly. BTW, I updated my input above.
@Nano: Your question still isn't clear at all. You talk about "the" List<int> but the point of a dictionary is that each key is a List<int>. Which one are you interested in? If you could provide a short but complete example of what you're talking about, it would be a lot easier to help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.