c# - 在 C# 中不使用 Add 方法将项目添加到 Dictionary

标签 c# dictionary

我有项目,我想在不使用 Add 方法的情况下将它们添加到 Dictionary(因为它会消耗行数)。有什么办法可以像这样将项目添加到字典中

new List<string>() { "P","J","K","L","M" };

或者像 List 中的 AddRange 方法。我们将不胜感激任何帮助。

最佳答案

引用自here

 Dictionary<int, StudentName> students = new Dictionary<int, StudentName>()
 {
   { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
   { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
   { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};

关于c# - 在 C# 中不使用 Add 方法将项目添加到 Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623843/

相关文章:

c# - 获取字典中的最后一个元素?

c# - 如何为特殊日子生成 Cron 表达式?

vb.net - 如何在vb.net中迭代字典?

python - 在 Python 中使用 csv 文件使用字典计算字符串中的单词数

c# - 在 c# winforms 程序中跟踪程序状态(登录等)的最佳方法

python - 在没有循环的情况下删除 O(1) 中双嵌套字典中的键及其值?

java - 实体中带有复合键的 Map<String, Entity>

c# - ASP.NET MVC 中的简单图形计算器,如 MS Windows 计算器

c# - 如何使用不同的字符编码构建项目?

c# - 矩阵到数组c#