如何将索引器应用于Dictionary.Values(C#3.0)

时间:2010-06-01 07:50:52

标签: c# c#-3.0 dictionary indexer

我做了一个程序

string[] arrExposureValues = stock.ExposureCollection[dt].Values.ToArray();
for(int i = 0; i < arrExposureValues.Length; i++)
    Console.WriteLine(arrExposureValues[i]);

没有错,工作正常。

但是可以做类似下面的事情

for(int i = 0; i < stock.ExposureCollection[dt].Count; i++)    
    Console.WriteLine(stock.ExposureCollection[dt].Values[i]);

这只是为了我的知识(基本上试图在一行中完成相同的工作)。

注意:ExposureCollection为Dictionary<DateTime, Dictionary<string, string>>

首先,如果可能的话,我有疑问!

我正在使用C#3.0。

感谢。

4 个答案:

答案 0 :(得分:1)

您可以使用foreach枚举stock.ExposureCollection [dt] .Keys列表。

foreach( string key in stock.ExposureCollection[dt].Keys ) {
  Console.WriteLine(stock.ExposureCollection[dt][key]);
}

答案 1 :(得分:1)

首先,你的任务是什么?枚举字典并同时枚举内部字典?

foreach(var item in dic)
{
    foreach(var inner in item.Value)
    {
        Console.WriteLine("key={0}, inner key={1}, inner value={2}",
            item.Key, inner.Key, inner.Value);
    }
}

答案 2 :(得分:0)

for(int i = 0; i < stock.ExposureCollection[dt].Count; i++)     
    Console.WriteLine(stock.ExposureCollection[dt].Values.ElementAt(i)); 

答案 3 :(得分:-1)

尝试SortedList - 它具有您可以索引的值和键等属性。