1

I have a list declared such as:

string[] myList = { "Item One", "Item Two", "Item Three" };

And a dictionary with one element, which value points towards above list:

Dictionary<string, object> myDictionary = new Dictionary<string, object>();
myDictionary.Add("DictionaryItem", myList);

I would like to print the contents of myList by pointing to the value in the dictionary. I have tried:

foreach (string element in myDictionary["DictionaryItem"])
{
    Console.WriteLine(element);
}

Returns syntax error:

foreach statement cannot operate on variables of type object because object does not contain a public definition for GetEnumerator.

How can I print myList, by pointing to the value of "DictionaryItem"?

August Williams
  • 907
  • 4
  • 20
  • 37
  • Possible duplicate of [What is the best way to iterate over a Dictionary in C#?](https://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c) – garfbradaz Jul 21 '17 at 15:21
  • Cast myDictionary["DictionaryItem"] to an array or what ever you need. – Vladimir Arustamian Jul 21 '17 at 15:24
  • 1
    Unless there is strict requirement, ideally try using strongly typed objects where ever possible, ex: `Dictionary` or `Dictionary` etc., in your case, to reduce unnecessary & costly Casts and you can get intellisense support as a bonus while in supported IDE. – Siva Gopal Jul 21 '17 at 15:36

4 Answers4

3

A foreach statement can only be used over an object inheriting IEnumerable. As the TValue of your dictionary is object, your foreach statement cannot compile even if it is actually an IEnumerable under the hood.

You have several choices to solve that issue:

Change your TValue

Best option, only if you can of course:

var myDictionary  = new Dictionary<string, string[]>();

Notice the keywork var in the variable definition. You can save a lot of time when instantiating objects like this.

Cast the result of myDictionary["DictionaryItem"] into an IEnumerable

Dangerous option if there is other types of object in your dictionary.

foreach (string element in (myDictionary["DictionaryItem"] as string[]))
{
    Console.WriteLine(element);
}

Remark: I am talking about IEnumerable and I use string[] in my options. That's because the C# array ([]) inherits from IEnumerable

fharreau
  • 2,105
  • 1
  • 23
  • 46
1

Not sure why you are referring to a string[] as an object in this example - are you looking to use object[] for the array? Either way the error is pretty explicit. You will need to use Dictionary<string, string[]> myDictionary

jProg2015
  • 1,098
  • 10
  • 40
1

It is just an object so foreach is not going to know how process it

string[] myList = (string[])myDictionary["DictionaryItem"];

foreach(string s in myList)
{
    Console.WriteLine(element);
}
David Thielen
  • 28,723
  • 34
  • 119
  • 193
paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

You could create a List of string from the String[] and iterate on that. Note that you want to iterate on the value of the dictionary item.

        string[] myList = { "Item One", "Item Two", "Item Three" };
        Dictionary<string, object> myDictionary = new Dictionary<string, object>();
        myDictionary.Add("DictionaryItem", myList);


        //Short Hand
        foreach (var item in new List<string>((string[])myDictionary.First(m => m.Key == "DictionaryItem").Value))
        {
            Console.WriteLine(item);
        }

        //or Long Hand version
        KeyValuePair<string, object> element = myDictionary.First(m => m.Key == "DictionaryItem");
        List<String> listItem = new List<string>((string[])element.Value);
        foreach (var item in listItem)
        {
            Console.WriteLine(item);
        }
Tony
  • 13
  • 4