【學習筆記】C# 字典

來源:互聯網
上載者:User

標籤:static   ons   包含   als   儲存   不包含   lin   nsvalue   using   

  • 字典
    • Dictionary是儲存鍵和值的集合
    • Dictionary是無序的,鍵Key是唯一的
  • 使用時,首先要引入泛型集合命名空間 using System.Collections.Generic;
  • 建立一個字典對象
    • Dictionary<key, value> dic = new Dictionary<key, value>();
    • Dictionary<string, int> dic = new Dictionary<string, int>();
  • Add方法,用來添加索引值對
    • dic.Add("小明", 13);
    • dic.Add("小紅", 15);
  • 通過 Remove() 方法,中設定Key 值 ,來移除value所在的索引值對
    • dic.Remove("小紅");
  • Clear()清空當前字典裡的所有內容
    • dic.Clear();
  • Count擷取當前字典裡value的個數
    • int count = dic.Count;
    • Console.WriteLine("當前字典中有 " + count + " 個索引值對!");
  • ContainsKey()方法查看當前字典裡是否含有Key-"小明"
    • bool b = dic.ContainsKey("小明");
    • if(b)
    • {
    • Console.WriteLine("存在小明的年齡!");
    • }else
    • {
    • Console.WriteLine("不存在小明的年齡!");
    • }
  • ContainsValue()方法直接查看是否存在當前的Value
    • bool b2 = dic.ContainsValue(10);
    • if (b2)
    • {
    • Console.WriteLine("存在年齡為10的人!");
    • }
    • else
    • {
    • Console.WriteLine("不存在年齡為10的人!");
    • }
  • TryGetValue()方法,嘗試擷取指定的Key所對應的Value
    • int s;
    • bool bb = dic.TryGetValue("xiaoming", out s);
    • //如果當前字典包含“小明”這個key,那麼就擷取對應的value並儲存在s中 ,bb=true
    • //如果當前字典不包含“小明”這個key,那麼s=null, bb = false;
  • 通過 Key 擷取 value
    • int age = dic["小明"];
    • Console.WriteLine("小明的年齡是: " + age);
  •   
     1 using System; 2 //引入泛型集合命名空間 3 using System.Collections.Generic; 4  5 namespace DictionaryDemo 6 { 7     class Program 8     { 9         static void Main(string[] args)10         {11             //建立一個字典對象,key 的類型是 string , value 的類型是int12             Dictionary<string, int> dic = new Dictionary<string, int>();13             //Add方法,用來添加索引值對14             dic.Add("小明", 13);15             dic.Add("小紅", 15);16 17 18             //通過 Remove() 方法,中設定Key 值 ,來移除value所在的索引值對19             dic.Remove("小紅");20 21             //Clear()清空當前字典裡的所有內容22             dic.Clear();23 24 25             //Count擷取當前字典裡value的個數26             int count = dic.Count;27             Console.WriteLine("當前字典中有 " + count + " 個索引值對!");28 29             //ContainsKey()方法查看當前字典裡是否含有Key-"小明"30             bool b = dic.ContainsKey("小明");31             if (b)32             {33                 Console.WriteLine("存在小明的年齡!");34             }35             else36             {37                 Console.WriteLine("不存在小明的年齡!");38             }39 40             //ContainsValue()方法直接查看是否存在當前的Value41             bool b2 = dic.ContainsValue(10);42             if (b2)43             {44                 Console.WriteLine("存在年齡為10的人!");45             }46             else47             {48                 Console.WriteLine("不存在年齡為10的人!");49             }50 51             //TryGetValue()方法,嘗試擷取指定的Key所對應的Value52             int s;53             bool bb = dic.TryGetValue("xiaoming", out s);54             //如果當前字典包含“小明”這個key,那麼就擷取對應的value並儲存在s中 ,bb=true55             //如果當前字典不包含“小明”這個key,那麼s=null, bb = false;56             if (bb)57             {58                 Console.WriteLine("包含“小明”這個key");59             }60             else61             {62                 Console.WriteLine("不包含“小明”這個key!");63             }64 65             //通過 Key 擷取 value66             int age = dic["小明"];67             Console.WriteLine("小明的年齡是: " + age);68         }69     }70 }

     

【學習筆記】C# 字典

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.