在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、关于匿名类型的特性 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转为List list.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集合 Dictionary<string, string> dic = new Dictionary<string, string>(); 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<string> list = new List<string>(); list.Add("String"); 注:任意,没有键/值 10、集合与LINQ检索表达式 using System.Linq; using System.Collections; int[] items = new int[] { 1, 2, 3, 4, 5 }; IEnumerable<int> 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]; } }
|
请发表评论