1: class CollectionInitializer : AppRunner.AbstractApplication
2: {
3: class StringCollection : ICollection<string>
4: {
5: public void Add(string item)
6: {
7: Console.WriteLine(item);
8: }
9:
10: // Other ICollection<T> Members
11: }
12:
13: public override void Run()
14: {
15: StringCollection strings = new StringCollection() { "Van's", "Brog", "Vicky" };
16: }
17: }
在这个示例中,编译器会自动为strings对象调用Add方法,以将初始值列表中的所有元素加入到集合中,这里我们只是简单将初始值列表中的元素输出到控制台。
第六个特性:Lambda表达式
C# 2.0中加入的匿名代理,简化了我们编写事件处理函数的工作,使我们不再需要单独声明一个函数来与事件绑定,只需要使用delegate关键字在线编写事件处理代码。
而C# 3.0则更进一步,通过Lambda表达式,我们可以一种更为简洁方式编写事件处理代码,新的Lambda事件处理代码看上去就像一个计算表达式,它使用"=>"符号来连接事件参数和事件处理代码。我可以这样写:SomeEvent += 事件参数 => 事件处理代码;下面是完整的示例:
1: delegate T AddDelegate<T>(T a, T b);
2:
3: class LambdaExpression : AppRunner.AbstractApplication
4: {
5: public static event EventHandler MyEvent;
6:
7: public override void Run()
8: {
9: MyEvent += delegate(object s, EventArgs e)
10: {
11: Console.WriteLine(s);
12: };
13:
14: MyEvent += (s, e) => { Console.WriteLine(s); };
15:
16: MyEvent(this, null);
17:
18: AddDelegate<string> add = (a, b) => a + b;
19:
20: Console.WriteLine(add("Lambda", "Expression"));
21: }
22: }
在上面的例子中,分别使用了匿名代理和Lambda表达式来实现同样的功能,可以明显看出Lambda表达式的实现更为简洁。我们在使用Lambda表达式编写事件处理代码时,无需指明事件参数的类型,且返回值就是最后一条语句的执行结果。
1: static class Extensions
2: {
3: public static int ToInt32(this string source)
4: {
5: return Int32.Parse(source);
6: }
7:
8: public static T[] Slice<T>(this T[] source, int index, int count)
9: {
10: if (index < 0 || count < 0 || index + count > source.Length)
11: {
12: throw new ArgumentException();
13: }
14:
15: T[] result = new T[count];
16: Array.Copy(source, index, result, 0, count);
17: return result;
18: }
19: }
20:
21: class ExtensionMethods : AppRunner.AbstractApplication
22: {
23: public override void Run()
24: {
25: string number = "123";
26: Console.WriteLine(number.ToInt32());
27:
28: int[] intArray = new int[] { 1, 2, 3 };
29:
30: intArray = intArray.Slice(1, 2);
31:
32: foreach (var i in intArray)
33: Console.WriteLine(i);
34: }
35: }
1: class LinqExpression : AppRunner.AbstractApplication
2: {
3: public override void Run()
4: {
5: // 定义匿名数组persons, 并为其赋初值
6: var persons = new[] {
7: new { Name="Van's", Sex=false, Age=22 },
8: new { Name="Martin", Sex=true, Age=30 },
9: new { Name="Jerry", Sex=false, Age=24 },
10: new { Name="Brog", Sex=false, Age=25 },
11: new { Name="Vicky", Sex=true, Age=20 }
12: };
13:
14:
15: /*
16: 执行简单Linq查询
17:
18: 检索所有年龄在24岁以内的人
19: 查询结果放在results变量中
20:
21: results变量的类型与数组persons相同
22: */
23: var results = from p in persons
24: where p.Age <= 24
25: select p;
26:
27: foreach (var person in results)
28: {
29: Console.WriteLine(person.Name);
30: }
31:
32: Console.WriteLine();
33:
34:
35: // 定义匿名数组customers, 并为其赋初值
36: // 该数组是匿名类型的
37: var customers = new[] {
38: new {
39: Name="Van's", City="China", Orders=new[] {
40: new {
41: OrderNo=0,
42: OrderName="C# Programming Language(Second Edition)",
43: OrderDate=new DateTime(2007,9, 5)
44: },
45: new {
46: OrderNo=1,
47: OrderName="Head First Design Patterns(Chinese Edition)",
48: OrderDate=new DateTime(2007,9,15)
49: },
50: new {
51: OrderNo=2,
52: OrderName="ASP.NET Unleashed 2.0(Chinese Edition)",
53: OrderDate=new DateTime(2007,09,18)
54: },
55: new {
56: OrderNo=3,
57: OrderName="The C++ Programming Langauge(Special Edition)",
58: OrderDate=new DateTime(2002, 9, 20)
59: }
60: }
61: },
62: new {
63: Name="Brog", City="China", Orders=new[] {
64: new {
65: OrderNo=0,
66: OrderName="C# Programming Language(Second Edition)",
67: OrderDate=new DateTime(2007, 9, 15)
68: }
69: }
70: },
71: new {
72: Name="Vicky", City="London", Orders=new[] {
73: new { OrderNo=0,
74: OrderName="C++ Programming Language(Special Edition)",
75: OrderDate=new DateTime(2007, 9, 20)
76: }
77: }
78: }
79: };
80:
81:
82: /*
83: 执行多重Linq查询
84:
85: 检索所在城市为中国, 且订单日期为2007年以后的所有记录
86: 查询结果是一个匿名类型的数组
87: 其中包含客户名, 订单号, 订单日期, 订单名四个字段
88: */
89: var someCustomers = from c in customers
90: where c.City == "China"
91: from o in c.Orders
92: where o.OrderDate.Year >= 2007
93: select new { c.Name, o.OrderNo, o.OrderDate, o.OrderName };
94:
95:
96: foreach (var customer in someCustomers)
97: {
98: Console.WriteLine(
99: customer.Name + ", " + customer.OrderName + ", " +
100: customer.OrderDate.ToString("D")
101: );
102: }
103: }
104: }
从上面的例子中,我们可以看到Linq查询的强大特性,它允许我们进行简单查询,或者进行更为复杂的多重连接查询。且查询的结果还可以是自定义的匿名类型。
以上是对C# 3.0中新增的八大特性的简要介绍,如果想了解更为深入的内容,可查看C# 3.0的官方规范。
相关文章:C#3.0 新特性速览(1)
posted @ 2007-09-29 13:52 van‘s 阅读(3018) 评论(18)
编辑