zoukankan      html  css  js  c++  java
  • Dictionary的基本用法

    1.创建泛型哈希表,然后加入元素 

    Dictionary<string,string> openWith=new Dictionary<string, string>();
                openWith.Add("txt","notepad.exe");
                openWith.Add("bmp","paint.exe");
                openWith.Add("dib","paint.exe");
                openWith.Add("rtf","wordpad.exe");

    2.遍历key

        foreach (string key in openWith.Keys)
                {
                    Console.WriteLine("Key = {0}", key);
                }

    3.遍历value

     foreach (string value in openWith.Values)
                {
                    Console.WriteLine("value = {0}", value);
                }

    4.遍历value, Second Method

    Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
                foreach (string s in valueColl)
                {
                    Console.WriteLine("Second Method, Value = {0}", s);
                }

    5.遍历字典

    foreach (KeyValuePair<string, string> kvp in openWith)
                {
                    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
                }

    6.添加存在的元素

    try
                {
                    openWith.Add("txt", "winword.exe");
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("An element with Key = "txt" already exists.");
                }

    7.删除元素

    openWith.Remove("doc");
                if (!openWith.ContainsKey("doc"))
                {
                    Console.WriteLine("Key "doc" is not found.");
                }

    8.判断键存在

    if (openWith.ContainsKey("bmp")) // True 
                {
                    Console.WriteLine("An element with Key = "bmp" exists.");
                }

    9.参数为其它类型

    Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
                OtherType.Add(1, "1,11,111".Split(','));
                OtherType.Add(2, "2,22,222".Split(','));
                Console.WriteLine(OtherType[1][2]);
  • 相关阅读:
    01-2制作U盘启动盘--装机助理工具
    01-1制作U盘启动盘--大白菜超级U盘启动盘制作工具
    计算机操作系统
    设置电脑系统密码以及桌面密码
    bios文字解释
    Word基本文档字体设置
    Ctrl/Alt 快捷键
    Windows键
    Laravel 5.2 四、.env 文件与模型操作
    Laravel 5.2 三、中间件、视图与 Blade 模板引擎
  • 原文地址:https://www.cnblogs.com/qingchengshiguang/p/12176450.html
Copyright © 2011-2022 走看看