C# Dictionary的使用实例代码
时间:2021-07-29 14:02:02
class Dirctonary
{
public void DictionaryGet()
{
Dictionary<int, string> productList = new System.Collections.Generic.Dictionary<int, string>();
productList.Add(1, "ProductionOne");
productList.Add(2, "ProductionTwo");
foreach (KeyValuePair<int, string> production in productList)
{
MessageBox.Show(string.Format("{0},{1}", production.Key, production.Value));
}
//MessageBox.Show(productList.Count.ToString());
//MessageBox.Show(productList[1].ToString());
Dictionary<int, string>.KeyCollection keys = productList.Keys;
foreach (var item in keys)
{
MessageBox.Show(item.ToString());
}
Dictionary<int, string>.ValueCollection collection = productList.Values;
foreach (var item in collection)
{
MessageBox.Show(string.Format("{0}", item));
}
//productList.Remove(1);
//productList.Clear();
MessageBox.Show("判断是否包含键值对中的键为”1“的值");
if (productList.ContainsKey(1))
{
MessageBox.Show(productList[1]);
}
MessageBox.Show("判断是否包含键值对中的值为”ProductionTwo“的值");
if (productList.ContainsValue("ProductionTwo"))
{
MessageBox.Show(string.Format("{0}", "this really exists"));
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring gateway + Oauth2实现单点登录及详细配置
![](https://img.aspxhome.com/file/2023/2/70942_0s.png)
Java中Arraylist动态扩容方法详解
![](https://img.aspxhome.com/file/2023/6/82026_0s.jpg)
Java DOM4J方式生成XML的方法
android中使用SharedPreferences进行数据存储的操作方法
![](https://img.aspxhome.com/file/2023/1/83531_0s.png)
Android编程使用GestureDetector实现简单手势监听与处理的方法
java GUI编程之布局控制器(Layout)实例分析
IDEA中使用jclasslib插件可视化方式查看类字节码的过程详解
![](https://img.aspxhome.com/file/2023/3/60923_0s.png)
分析Java中的类加载问题
![](https://img.aspxhome.com/file/2023/6/94376_0s.jpg)
Java聊天室之实现客户端一对一聊天功能
![](https://img.aspxhome.com/file/2023/0/79340_0s.png)
JetCache 缓存框架的使用及源码解析(推荐)
![](https://img.aspxhome.com/file/2023/2/64402_0s.png)
Java读文件修改默认换行符的实现
Java lambda表达式与泛型整理总结
springboot使用事物注解方式代码实例
C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法
关于Java反编译字节码文件
初步编写IDEA\\AndroidStudio翻译插件的方法
![](https://img.aspxhome.com/file/2023/9/96029_0s.png)
详解SpringBoot注册Windows服务和启动报错的原因
![](https://img.aspxhome.com/file/2023/1/63431_0s.jpg)
Android中使用CircleImageView和Cardview制作圆形头像的方法
![](https://img.aspxhome.com/file/2023/2/92582_0s.gif)