using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; /**////<summary> /// AutomaticProperties(自动属性)的摘要说明 ///</summary> publicclass AutomaticProperties { publicint ID { get; set; } // 上面的ID属性(自动属性)等同于下面的ID属性 // private int _id; // public int ID // { // get { return _id; } // set { _id = value; } // } }
ObjectInitializers.cs(对象初始化器)
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; /**////<summary> /// ObjectInitializers(对象初始化器)的摘要说明 ///</summary> publicclass ObjectInitializers { publicint ID { get; set; } publicstring Name { get; set; } publicvoid ObjectInitializersTest() { ObjectInitializers oi =new ObjectInitializers { ID =1, Name ="zhaosoft" }; // 上面的oi对象(对象初始化器)等同于下面的oi对象 // ObjectInitializers oi = new ObjectInitializers(); // oi.ID = 1; // oi.Name = "zhaosoft"; } }
CollectionInitializers.cs(集合初始化器)
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; using System.Collections.Generic; /**////<summary> /// CollectionInitializers(集合初始化器)的摘要说明 ///</summary> publicclass CollectionInitializers { publicint ID { get; set; } publicstring Name { get; set; } publicvoid CollectionInitializersTest() { List<CollectionInitializers> list =new List<CollectionInitializers> { new CollectionInitializers { ID =1, Name ="zhaosoft" }, new CollectionInitializers { ID =2, Name ="zhaosoft" }, new CollectionInitializers { ID =3, Name ="zhaosoft" } }; // 上面的list集合(集合初始化器)等同于下面的list集合 // List<CollectionInitializers> list = new List<CollectionInitializers>(); // list.Add(new CollectionInitializers { ID = 1, Name = "zhaosoft" }); // list.Add(new CollectionInitializers { ID = 2, Name = "zhaosoft" }); // list.Add(new CollectionInitializers { ID = 3, Name = "zhaosoft" }); } }
ExtensionMethods.cs(扩展方法)
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; /**////<summary> /// ExtensionMethods 的摘要说明 ///</summary> publicclass ExtensionMethods { publicvoid ExtensionMethodsTest() { string s ="123"; // 使用string的ToInt32()扩展方法 int i = s.ToInt32(); // i == 123 string[] ary =newstring[] { "a", "b", "c" }; // 使用object的In()扩展方法 bool b ="b".In(ary); // b == true } } /**////<summary> /// 扩展方法(类和方法均为static) /// 使用的时候要引用该类的命名空间 ///</summary> publicstaticclass MyExtensionMethods { // this代表扩展方法应用于string类型上 // ToInt32()是将string类型转换为int类型的扩展方法 publicstaticint ToInt32(thisstring s) { int i; Int32.TryParse(s, out i); return i; } // this代表扩展方法应用于object类型上 // 该扩展方法需要一个类型为System.Collections.IEnumerable的参数 // In()是判断一个object是否存在于一个System.Collections.IEnumerable中的扩展方法 publicstaticbool In(thisobject o, System.Collections.IEnumerable e) { foreach (object i in e) { if (i.Equals(o)) { returntrue; } } returnfalse; } }