【发布时间】:2011-01-25 00:42:38
【问题描述】:
我创建了一个 Dictionary<string, string> 集合,以便我可以通过它们的字符串标识符快速引用这些项目。
但我现在还需要通过 index counter 来访问这个集合(foreach 在我的实际示例中不起作用)。
我必须对下面的集合执行什么操作,以便我也可以通过整数索引访问它的项目?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDict92929
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> events = new Dictionary<string, string>();
events.Add("first", "this is the first one");
events.Add("second", "this is the second one");
events.Add("third", "this is the third one");
string description = events["second"];
Console.WriteLine(description);
string description = events[1]; //error
Console.WriteLine(description);
}
}
}
【问题讨论】:
标签: c# dictionary indexing