2010-06-01 19 views
1

我做了一個程序如何申請索引器Dictionary.Values(C#3.0)

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。

謝謝。

回答

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); 
    } 
} 
-1

嘗試排序列表 - 它有一個像值和鍵的屬性,您可以索引。

1

你可以列舉在stock.ExposureCollection使用foreach [DT] .Keys名單。

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

+1避免嵌套循環 – 2010-06-01 08:56:56

+0

@Enrico坎皮多利奧:這裏有3個備用循環!此外內在關鍵不是abaliable – abatishchev 2010-06-01 09:17:19

+0

@abatishchev你能發展你的觀點嗎? – 2010-06-01 09:32:12

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