.Net中泛型類型Dictionary用法

獲取Dictionary字典數(shù)據(jù)總數(shù)  

刪除一個鍵

循環(huán)字典數(shù)據(jù)里的值

查找鍵是否存在 

......


protected void Page_Load(object sender, EventArgs e)
    {
        //泛型Dictionary  
        Dictionary<string, string> dit = new Dictionary<string, string>();
        dit.Add("13", "Jsons");
        dit.Add("22", "Jsons.cn");
        Response.Write("總數(shù)"   dit.Count   "<br/>");//字典數(shù)據(jù)總數(shù)  
        dit.Remove("13");//刪除一個鍵  
        if (!dit.ContainsKey("13"))
        {
            dit.Add("13", "在線工具");
        }//判讀如果不包含指定的鍵則添加  
        foreach (KeyValuePair<string, string> kvp in dit)
        {
            Response.Write(kvp.Key);
            Response.Write("====="   kvp.Value);
            Response.Write("<br/>");
        }//循環(huán)字典中數(shù)據(jù)  

        Dictionary<string, string>.KeyCollection ditkey = dit.Keys;
        foreach (string k in ditkey)
        {
            Response.Write(k   "<br/>");
        }//循環(huán)字典數(shù)據(jù)的鍵  

        Dictionary<string, string>.ValueCollection ditvalue = dit.Values;
        foreach (var v in ditvalue)
        {
            Response.Write(v   "<br/>");
        }//循環(huán)字典數(shù)據(jù)里的值  

        foreach (var ditk in dit.Keys)
        {
            Response.Write(ditk   "<br/>");
        }//另一種獲取字典鍵的方法  

        string f = dit["13"];
        Response.Write(f);//根據(jù)鍵 獲取值  

        string s = string.Empty;
        if (dit.TryGetValue("13", out s))
        {
            Response.Write("<br/>找到");
        }
        else
        {
            Response.Write("<br/>未找到");
        }//查找鍵是否存在  

        //泛型List 類型  
        List<string> a = new List<string>();
        a.Add("aa");
        a.Add("bb");
        foreach (string b in a)
        {
            Response.Write("<br/>"   b   "<br/>");
        }
        //泛型IList  
        IList<string> Il = new List<string>();
        Il.Add("11");
        Il.Add("22");
        foreach (var i in Il)
        {
            Response.Write(i   "<br/>");
        }
    }


原文鏈接:.Net中泛型類型Dictionary用法