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

标签:Dictionary,使用
0
投稿

猜你喜欢

  • Spring gateway + Oauth2实现单点登录及详细配置

    2022-12-19 00:16:49
  • Java中Arraylist动态扩容方法详解

    2023-04-30 23:25:53
  • Java DOM4J方式生成XML的方法

    2022-07-19 02:32:42
  • android中使用SharedPreferences进行数据存储的操作方法

    2023-06-16 17:37:42
  • Android编程使用GestureDetector实现简单手势监听与处理的方法

    2021-11-18 17:07:43
  • java GUI编程之布局控制器(Layout)实例分析

    2023-11-23 13:10:35
  • IDEA中使用jclasslib插件可视化方式查看类字节码的过程详解

    2021-10-12 08:18:31
  • 分析Java中的类加载问题

    2023-09-03 19:37:04
  • Java聊天室之实现客户端一对一聊天功能

    2022-12-28 09:23:30
  • JetCache 缓存框架的使用及源码解析(推荐)

    2021-07-23 12:21:25
  • Java读文件修改默认换行符的实现

    2023-11-29 08:24:32
  • Java lambda表达式与泛型整理总结

    2021-07-15 19:57:14
  • springboot使用事物注解方式代码实例

    2022-07-09 00:13:21
  • C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法

    2023-01-30 04:11:58
  • 关于Java反编译字节码文件

    2021-10-07 01:41:32
  • 初步编写IDEA\\AndroidStudio翻译插件的方法

    2022-12-14 19:30:30
  • 详解SpringBoot注册Windows服务和启动报错的原因

    2022-12-28 17:10:09
  • Android中使用CircleImageView和Cardview制作圆形头像的方法

    2022-04-19 05:41:35
  • Java中通过jsch来连接远程服务器执行linux命令

    2023-02-02 22:21:52
  • Spring Boot缓存实战 Caffeine示例

    2021-11-15 17:39:34
  • asp之家 软件编程 m.aspxhome.com