我有一个列表中声明,如:如何通过字典打印列表的内容?
string[] myList = { "Item One", "Item Two", "Item Three" };
并与一个元素,一个词典这对上面所列内容价值点:
Dictionary<string, object> myDictionary = new Dictionary<string, object>();
myDictionary.Add("DictionaryItem", myList);
我想通过指向要打印的myList
内容字典中的值。我曾尝试:
foreach (string element in myDictionary["DictionaryItem"])
{
Console.WriteLine(element);
}
返回语法错误:
foreach statement cannot operate on variables of type object because object does not contain a public definition for GetEnumerator.
如何打印myList
,所指向的"DictionaryItem"
价值?
可能重复[什么是在C#中迭代字典的最佳方式?](https://stackoverflow.com/questions/141088/what-is-the-最好的方式迭代在字典中的c) – garfbradaz
铸造myDictionary [“DictionaryItem”]到一个数组或你需要什么。 –
除非有严格的要求,否则理想情况下尽量使用强类型对象,例如'Dictionary'或'Dictionary '等等,以减少不必要和昂贵的强制转换,您可以在受支持的IDE中获得智能感知支持作为奖励。 –