自动属性(Automatic Properties) <o:p></o:p>
<o:p></o:p>定义Entity现在可以像接口的属性一样:<o:p></o:p>
public class People<o:p></o:p>
{<o:p></o:p>
public string Code<o:p></o:p>
{<o:p></o:p>
get;<o:p></o:p>
set;<o:p></o:p>
} <o:p></o:p>
<o:p> </o:p>
public string Name<o:p></o:p>
{<o:p></o:p>
get;<o:p></o:p>
set;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
将在编译时自动生成私有变量。<o:p></o:p>
对象初始化器(Object Initializers)<o:p></o:p>
可以这样来生成Entity的实例:<o:p></o:p>
People p = new People { Code = "110", Name = "WantSong" };<o:p></o:p>
集合初始化器(Collection Initializers)
可以这样来生成集合<o:p></o:p>
List<People> lst = new List<People>{<o:p></o:p>
new People{Code = "110", Name = "Booch"},<o:p></o:p>
new People{Code = "111", Name = "Scoot"},<o:p></o:p>
new People{Code = "112", Name = "Tom"},<o:p></o:p>
};
<o:p> </o:p>
据Scott Guthrie说这样可以省很多字。<o:p></o:p>
<o:p>扩展方法 (Extension Methods)</o:p>
定义一个校验姓名的类。
namespace MyLinqStudy.Extension<o:p></o:p>
{<o:p></o:p>
public static class NameValidation<o:p></o:p>
{ <o:p></o:p>
public static bool IsValidName(this string s)<o:p></o:p>
{ <o:p></o:p>
Regex regex = new Regex(@"[\u4e00-\u9fa5]{2,5}|(^[a-zA-Z]+[\s.]?([a-zA-Z]+[\s.]?){0,4}[a-zA-Z]$)");<o:p></o:p>
return regex.IsMatch(s);<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
}
<o:p> </o:p>
Using了该Space后,string 的方法被扩展了:
扩展的关键是IsValidName方法中有一个关键字“this”,告诉编译器将该方法扩展到String类中。<o:p></o:p>
打开Enumerable,可以看到很多这样的Extension。
<o:p> </o:p>
也照着写一个While:
namespace MyLinqStudy.Extension<o:p></o:p>
{<o:p></o:p>
public static class WhileExtension<o:p></o:p>
{<o:p></o:p>
public static IEnumerable<TSource> While<TSource>(this IEnumerable<TSource> source, <o:p></o:p>
Func<TSource, bool> predicate)<o:p></o:p>
{<o:p></o:p>
foreach(TSource item in source)<o:p></o:p>
{<o:p></o:p>
if (predicate(item))<o:p></o:p>
{<o:p></o:p>
yield return item;<o:p></o:p>
}<o:p></o:p>
} <o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
跑一下看看:<o:p></o:p>
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };<o:p></o:p>
IEnumerable<int> numQuery = numbers.While(num => num < 3);<o:p></o:p>
foreach (int j in numQuery)<o:p></o:p>
{<o:p></o:p>
Console.Write("{0,1} ", j);<o:p></o:p>
}<o:p></o:p>
num => num < 3 是什么东东? 看下一节Lambda表达式。<o:p></o:p>
<o:p> </o:p>
Lambda表达式
num => num < 3 是一个Lambda表达式,表示取小于3的数据。上节中的numQuery在2.0中我们这样写:<o:p></o:p>
IEnumerable<int> numQuery = numbers.While(<o:p></o:p>
delegate(int i)<o:p></o:p>
{<o:p></o:p>
return i < 3 ? true : false;<o:p></o:p>
}<o:p></o:p>
);<o:p></o:p>
Lambda表达式格式为:<o:p></o:p>
(参数列表) => 表达式或者语句块<o:p></o:p>
参数列表可以有多个,多个时用()括起来;参数类型可以是显式也可以是隐式;表达式、语句块就是执行操作的主体。<o:p></o:p>
试试下面这段代码<o:p></o:p>
delegate bool CompareDelegate1();<o:p></o:p>
delegate bool CompareDelegate2(int i,int j,int k);<o:p></o:p>
class Program<o:p></o:p>
{<o:p></o:p>
static void Main(string[] args)<o:p></o:p>
{<o:p></o:p>
int compare = 11;<o:p></o:p>
CompareDelegate1 mydel1 = () => {compare = 0; return compare > 10; };<o:p></o:p>
Console.WriteLine("compare = {0};result={1}", compare,mydel1());<o:p></o:p>
<o:p> </o:p>
CompareDelegate2 mydel2 = (x, y, z) => { return x == y && y== z; };<o:p></o:p>
Console.WriteLine("x= {0},y= {1},z= {2}; result = {3}", 8,8,8, mydel2(8,8,8));
}
}<o:p></o:p>
注意:
· A variable that is captured will not be garbage-collected until the delegate that references it goes out of scope.<o:p></o:p>
· Variables introduced within a lambda expression are not visible in the outer method.<o:p></o:p>
· A lambda expression cannot directly capture a ref or out parameter from an enclosing method.<o:p></o:p>
· A return statement in a lambda expression does not cause the enclosing method to return.<o:p></o:p>
· A lambda expression cannot contain a goto statement, break statement, or continue statement whose target is outside the body or in the body of a contained anonymous function.<o:p></o:p>
<o:p> </o:p>