[源码下载]
步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
作者:webabcd
介绍
·Automatic Properties(自动属性)
·Object Initializers(对象初始化器)
·Collection Initializers(集合初始化器)
·Extension Methods(扩展方法)
示例
AutomaticProperties.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>
/// AutomaticProperties(自动属性)的摘要说明
/// </summary>
public class AutomaticProperties
{
public int ID { get; set; }
// 上面的ID属性(自动属性)等同于下面的ID属性
// private int _id;
// public int ID
// {
// get { return _id; }
// set { _id = value; }
// }
}
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>
public class AutomaticProperties
{
public int 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>
public class ObjectInitializers
{
public int ID { get; set; }
public string Name { get; set; }
public void ObjectInitializersTest()
{
ObjectInitializers oi = new ObjectInitializers { ID = 1, Name = "webabcd" };
// 上面的oi对象(对象初始化器)等同于下面的oi对象
// ObjectInitializers oi = new ObjectInitializers();
// oi.ID = 1;
// oi.Name = "webabcd";
}
}
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>
public class ObjectInitializers
{
public int ID { get; set; }
public string Name { get; set; }
public void ObjectInitializersTest()
{
ObjectInitializers oi = new ObjectInitializers { ID = 1, Name = "webabcd" };
// 上面的oi对象(对象初始化器)等同于下面的oi对象
// ObjectInitializers oi = new ObjectInitializers();
// oi.ID = 1;
// oi.Name = "webabcd";
}
}
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>
public class CollectionInitializers
{
public int ID { get; set; }
public string Name { get; set; }
public void CollectionInitializersTest()
{
List<CollectionInitializers> list = new List<CollectionInitializers>
{
new CollectionInitializers { ID = 1, Name = "webabcd" },
new CollectionInitializers { ID = 2, Name = "webabcdefg" },
new CollectionInitializers { ID = 3, Name = "webabcdefghijklmn" }
};
// 上面的list集合(集合初始化器)等同于下面的list集合
// List<CollectionInitializers> list = new List<CollectionInitializers>();
// list.Add(new CollectionInitializers { ID = 1, Name = "webabcd" });
// list.Add(new CollectionInitializers { ID = 2, Name = "webabcdefg" });
// list.Add(new CollectionInitializers { ID = 3, Name = "webabcdefghijklmn" });
}
}
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>
public class CollectionInitializers
{
public int ID { get; set; }
public string Name { get; set; }
public void CollectionInitializersTest()
{
List<CollectionInitializers> list = new List<CollectionInitializers>
{
new CollectionInitializers { ID = 1, Name = "webabcd" },
new CollectionInitializers { ID = 2, Name = "webabcdefg" },
new CollectionInitializers { ID = 3, Name = "webabcdefghijklmn" }
};
// 上面的list集合(集合初始化器)等同于下面的list集合
// List<CollectionInitializers> list = new List<CollectionInitializers>();
// list.Add(new CollectionInitializers { ID = 1, Name = "webabcd" });
// list.Add(new CollectionInitializers { ID = 2, Name = "webabcdefg" });
// list.Add(new CollectionInitializers { ID = 3, Name = "webabcdefghijklmn" });
}
}
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>
public class ExtensionMethods
{
public void ExtensionMethodsTest()
{
string s = "123";
// 使用string的ToInt32()扩展方法
int i = s.ToInt32();
// i == 123
string[] ary = new string[] { "a", "b", "c" };
// 使用object的In()扩展方法
bool b = "b".In(ary);
// b == true
}
}
/// <summary>
/// 扩展方法(类和方法均为static)
/// 使用的时候要引用该类的命名空间
/// </summary>
public static class MyExtensionMethods
{
// this代表扩展方法应用于string类型上
// ToInt32()是将string类型转换为int类型的扩展方法
public static int ToInt32(this string s)
{
int i;
Int32.TryParse(s, out i);
return i;
}
// this代表扩展方法应用于object类型上
// 该扩展方法需要一个类型为System.Collections.IEnumerable的参数
// In()是判断一个object是否存在于一个System.Collections.IEnumerable中的扩展方法
public static bool In(this object o, System.Collections.IEnumerable e)
{
foreach (object i in e)
{
if (i.Equals(o))
{
return true;
}
}
return false;
}
}
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>
public class ExtensionMethods
{
public void ExtensionMethodsTest()
{
string s = "123";
// 使用string的ToInt32()扩展方法
int i = s.ToInt32();
// i == 123
string[] ary = new string[] { "a", "b", "c" };
// 使用object的In()扩展方法
bool b = "b".In(ary);
// b == true
}
}
/// <summary>
/// 扩展方法(类和方法均为static)
/// 使用的时候要引用该类的命名空间
/// </summary>
public static class MyExtensionMethods
{
// this代表扩展方法应用于string类型上
// ToInt32()是将string类型转换为int类型的扩展方法
public static int ToInt32(this string s)
{
int i;
Int32.TryParse(s, out i);
return i;
}
// this代表扩展方法应用于object类型上
// 该扩展方法需要一个类型为System.Collections.IEnumerable的参数
// In()是判断一个object是否存在于一个System.Collections.IEnumerable中的扩展方法
public static bool In(this object o, System.Collections.IEnumerable e)
{
foreach (object i in e)
{
if (i.Equals(o))
{
return true;
}
}
return false;
}
}
OK
[源码下载]
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344909,如需转载请自行联系原作者