开发者

How can I apply indexer to Dictionary.Values (C# 3.0)

I have done a program

string[] arrExposureValues = stock.ExposureCollection[dt].Values.ToArray();
for(int i = 0; i < arrExposureValues.Length; i++)
    Console.WriteLine(开发者_运维知识库arrExposureValues[i]);

Nothing wrong and works fine.

But is it possible to do something like the below

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

This is just for my sake of knowledge (Basically trying to accomplish the same in one line).

Note: ExposureCollection is Dictionary<DateTime, Dictionary<string, string>>

First of all I have the doubt if it is at all possible!

I am using C# 3.0.

Thanks.


First of all, what is you task? Enumerate dictionary and enumerate inner dictionary in the same time?

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


You could enumerate over the stock.ExposureCollection[dt].Keys list using a foreach.

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


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


Try SortedList - it has properties like Values and Keys which you can index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜