联系我们

为客户提供超预期的高品质网站设计!

ASP.NET 泛型类型 Dictionary操作

2018-2-21 0:00:00

protected void Page_Load(object sender, EventArgs e)
    {
        //泛型Dictionary 
        Dictionary<string, string> dit = new Dictionary<string, string>();
        dit.Add("13", "张三");
        dit.Add("22", "李四");
        Response.Write("总数" + dit.Count + "<br/>");//字典数据总数 
        dit.Remove("13");//删除一个键 
        if (!dit.ContainsKey("13"))
        {
            dit.Add("13", "张三1");
        }//判读如果不包含指定的键则添加 


        foreach (KeyValuePair<string, string> kvp in dit)
        {
            Response.Write(kvp.Key);
            Response.Write("=====" + kvp.Value);
            Response.Write("<br/>");
        }//循环字典中数据 


        Dictionary<string, string>.KeyCollection ditkey = dit.Keys;
        foreach (string k in ditkey)
        {
            Response.Write(k + "<br/>");
        }//循环字典数据的键 


        Dictionary<string, string>.ValueCollection ditvalue = dit.Values;
        foreach (var v in ditvalue)
        {
            Response.Write(v + "<br/>");
        }//循环字典数据里的值 


        foreach (var ditk in dit.Keys)
        {
            Response.Write(ditk + "<br/>");
        }//另一种获取字典键的方法 


        string f = dit["13"];
        Response.Write(f);//根据键 获取值 


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

//循环

 foreach (var detailItem in Dict_detail)
            {

                Response.Write(detailItem.Value + "<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/>");
        }
    }

 

上一条新闻:repeater控件中button按钮事件的使用
下一条新闻:Jquery 填充表格

 

400-6137-308 54057491 info@maolian.net