1. 程式人生 > >【學習筆記】C# 字典

【學習筆記】C# 字典

鍵值對 保存 包含 ear 是否 nod 對象 命名空間 不包含

  • 字典
    • 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 的類型是int
    12             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             else
    36             {
    37                 Console.WriteLine("不存在小明的年齡!");
    38             }
    39 
    40             //ContainsValue()方法直接查看是否存在當前的Value
    41             bool b2 = dic.ContainsValue(10);
    42             if (b2)
    43             {
    44                 Console.WriteLine("存在年齡為10的人!");
    45             }
    46             else
    47             {
    48                 Console.WriteLine("不存在年齡為10的人!");
    49             }
    50 
    51             //TryGetValue()方法,嘗試獲取指定的Key所對應的Value
    52             int s;
    53             bool bb = dic.TryGetValue("xiaoming", out s);
    54             //如果當前字典包含“小明”這個key,那麽就獲取對應的value並保存在s中 ,bb=true
    55             //如果當前字典不包含“小明”這個key,那麽s=null, bb = false;
    56             if (bb)
    57             {
    58                 Console.WriteLine("包含“小明”這個key");
    59             }
    60             else
    61             {
    62                 Console.WriteLine("不包含“小明”這個key!");
    63             }
    64 
    65             //通過 Key 獲取 value
    66             int age = dic["小明"];
    67             Console.WriteLine("小明的年齡是: " + age);
    68         }
    69     }
    70 }

【學習筆記】C# 字典