c# - 将字典分配给键值

标签 c# asp.net

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

如何分配 loadData(key,value)在 C# 中,这样我就可以访问 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);
}

关于c# - 将字典分配给键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17470915/

相关文章:

javascript - Javascript 中的正则表达式用于 trim 链接

asp.net - 我如何知道是否选择了选项卡?

ASP.NET - FormView - 数据源仅更新部分字段

c# - C# 中的 Console.ReadKey(false) 有点可疑

c# - 如何在 StyleCop 中执行私有(private)方法的文档?

c# - 可以安全地假设由 new 初始化的所有 C# 变量都是引用吗?

c# - 从 winforms 捕获 ASP.NET 网页中的错误

c# - Visual Studio 2015 Update 3 中 ASP.NET Core v1.0 的浏览器链接

javascript - 光 slider 数据拇指图像路径未使用 Eval() 从数据库绑定(bind)

c# - 使用 BasicHttpBinding 检索和更新数据的最快方法