Asp.net mvc 2 如何向字典提供硬编码值

Asp.net mvc 2 如何向字典提供硬编码值,asp.net-mvc-2,Asp.net Mvc 2,这是我的字典 Dictionary<string, Test> test = new Dictionary<string, Test>(); 字典测试=新建字典(); 在测试中,我有Id、姓名、分数。我想用硬编码的值填充这些属性。(不止一个计数) 如何为字典提供静态值 我是否应该这样给予 Dictionary<string, Test> test= new Dictionary<string, Test>(); Groups groups =

这是我的字典

Dictionary<string, Test> test = new Dictionary<string, Test>();
字典测试=新建字典();
在测试中,我有Id、姓名、分数。我想用硬编码的值填充这些属性。(不止一个计数)

如何为字典提供静态值


我是否应该这样给予

Dictionary<string, Test> test= new Dictionary<string, Test>();
Groups groups = new Groups();
groups.Id = "1";
groups.Name = "Name";
groups.Description = "Desc";
test.Add(groups.Id, groups);
字典测试=新建字典();
组=新组();
组。Id=“1”;
groups.Name=“Name”;
groups.Description=“Desc”;
test.Add(groups.Id,groups);

您可以在C#3&4中使用集合初始化器语法:


我是否应该这样给……字典测试=新字典();var Data=新字典{{1,“A”,“A”},{2,“B”,“B”},}@罗比尼奥:不,那不行。内大括号的第一个成员是键,第二个成员是值。每对中的第二个成员需要能够初始化
测试
,这取决于
测试
的定义。最简单的方法是调用构造函数。文字还可以与定义的适当隐式转换一起使用。
var data = new Dictionary<String,Object> {
  { "Foo", "Bax" },
  { "Bar", new DateTime(2010,10,10) },
  { "Xyzzy", 42 }
};
Dictionary<string, Test> test= new Dictionary<string, Test>();
Groups groups = new Groups();
groups.Id = "1";
groups.Name = "Name";
groups.Description = "Desc";
test.Add(groups.Id, groups);
Dictionary<string, Test> test= new Dictionary<string, Groups> {
  {
    "1", new Groups {
      Id = "1",
      Name = "Name",
      Description = "Desc"
    }
  }, {
    "2", new Groups {
      Id = "2",
      Name = "Another Name",
      Description = "Something        }
  }
};