一、关于匿名类型的特性1、匿名类型定义:匿名对象var vobj = new { id = "编号", title = "标题", content = "内容" };访问:var id = vobj.id;结果:编号2、结合匿名数组应用定义:集合初始化器var article = new[]{ new { id = "编号一", title = "标题一", content = "内容一" } new { id = "编号二", title = "标题二", content = "内容二" }};访问:var title = article[1].title;结果:标题二3、结合匿名集合List集合定义:var list = article.ToList(); //Array转为Listlist.Add(new { id = "编号三", title = "标题三", content = "内容三" });article = list.ToArray(); //List转为Array访问:var content = article[2].content;结果:内容三4、结合JavaScriptSerializer序列化var str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);结果:JSON字符串5、结合结合控件DropDownList ddl = new DropDownList();ddl.DataSource = list;ddl.DataTextField = "title";ddl.DataValueField = "id";ddl.DataBind();注:var关键字是C# 3.0开始新增的特性,只能用于局部变量使用(隐式类型变量)6、Dictionary集合Dictionarydic = new Dictionary ();dic.Add("key", "value");注:键不允许重复7、NameValueCollection集合NameValueCollection col = new NameValueCollection(); col.Add("key", "value");注:如果键值相同则合并8、Hashtable集合Hashtable ht = new Hashtable(); ht.Add("key","value");注:键不允许重复9、List集合List list = new List ();list.Add("String");注:任意,没有键/值10、集合与LINQ检索表达式using System.Linq;using System.Collections; int[] items = new int[] { 1, 2, 3, 4, 5 };IEnumerable ints = from item in items where item > 2.5 select item;foreach (var p in ints){ Console.WriteLine(p);} 关于:IEnumerable实例应用:通过Linq查找再遍历,然后以JSON的格式输出到客户端;11、集合迭代器public System.Collections.IEnumerator GetEnumerator(){ string[] days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; for (int i = 0; i < days.Length; i++) { yield return days[i]; }}