C#2.0 泛型

http://www.cnblogs.com/abatei/archive/2008/02/20/1075760.html

泛型优点:
1,操作中避免强制转换或装箱操作的成本或风险 2,安全高效

实际使用:
1,泛型List<T>的运算速度比ArrayList的快,能用ArrayList的地方都可以用List<T>取代
        List<int> ary = new List<int>();
        for (int i = 0; i < count; i++)
        {
            ary.Add(i);
        }


2,能方便的产生键值对Dictionary<T,T>
        Dictionary<int,string> dict=new Dictionary<int,string>();
        dict.Add(0, "aa"); dict.Add(1, "bb"); dict.Add(2, "cc");
        Response.Write("dict[0]=" + dict[0] + "<br>");
        dict[0] = "我已经修改了";
        Response.Write("dict[0]=" + dict[0] + "<br>");
        dict.Remove(0);

        foreach (KeyValuePair<int, string> kv in dict)
        {
            Response.Write("key="+kv.Key+",value="+kv.Value+"<br>");
        }

3、System.Collections.Specialized.HybridDictionary
集合长度少于10时候效率有优先于System.Collections.Generic.Dictionary<string, object>(length)

你可能感兴趣的:(C#)