Dictionary<int, String> loadData = new Dictionary();
// File contains a list of filenames say for ex. 5 filenames
foreach (var file in filenames)
{
// I want to assign like this
loadData[id]=file;
}
如何在C#中分配
loadData(key,value)
,以便像loadData[5] = "hello.txt"
一样进行访问; 最佳答案
首先:您正在使用new Dictionary()
,它将无法正常工作。请改用new Dictionary<int, String>()
。
第二:id
来自哪里?如果只是柜台,您可以执行以下操作:
Dictionary<int, String> loadData = new Dictionary<int, String>();
int id = 0;
foreach (string file in filenames)
{
loadData.Add(id++, file);
}