0

this is my code

pritvate static Dictionary<int, string[]> _list = new Dictionary<int, string[]>();

how can i get the string[] out of this? I have tried this and a lot more:

string[] s = _list.Values;

but it is all not working.

please help

5
  • Use the indexer _list[someKey]. Commented Jun 6, 2018 at 15:22
  • Provide a key value: var key=7; string[] s = _list[key]; Commented Jun 6, 2018 at 15:22
  • 1
    This cannot work, because _list.Values is of a type similar to string[][]. Maybe you really want to have a Dictionary<int, string>? Commented Jun 6, 2018 at 15:22
  • You should read documentation about dictionnary I think you did not understand the concept. Commented Jun 6, 2018 at 15:23
  • 1
    Trial and error isnt always the best approach, as @Nerevar already pointed out you should read the documentation. Commented Jun 6, 2018 at 15:41

1 Answer 1

3

If you want all string arrays for all keys merged into a single array, you can use LINQ's .SelectMany(...):

var strings = _list.Values.SelectMany(v => v).ToArray()

Reading your question again, I wonder if you're asking how to access a value for a single key. So, if you want the string array for a single key you can simply use the indexer:

var value = _list["keyname"];

But that will cause an exception if the key doesn't exist. If you're not sure that the key exists, you can use .TryGetValue(...):

string[] value;
if (_list.TryGetValue("keyname", out value))
{
    // value was found
}
else
{
    // value wasn't found
}
Sign up to request clarification or add additional context in comments.

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.