views:

37

answers:

1

Hello.

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);
}
+3  A: 

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.

Jon Skeet
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`?
Kobi
@Jon, 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 HE
@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.
Jon Skeet