Skip to main content
 首页 » 编程设计

c#之你如何使用集合初始值设定项来创建新字典

2024年01月13日28lori

如果你可以在列表中做到这一点

List<int> a = new List<int>() { 
   2, 4, 6, 8, 10 
}; 

你怎么能在字典中做同样的事情?

Dictionary<int, bool> b = new Dictionary<int, bool>() { 
   ?, ?, ? 
}; 

请您参考如下方法:

Dictionary<int, bool> b = new Dictionary<int, bool>() { 
   {1, true},{2, false},{3, true} 
};