C#2.0時可以使用匿名方法:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace ConsoleApplication3
7{
8 public delegate int Calculate(int a, int b);
9 class Program
10 {
11
12 static void Main(string[] args)
13 {
14 int a = 3;
15 int b = 4;
16 Calculate result = delegate(int ta, int tb) { return ta + tb; };
17
18 Console.WriteLine(result(a,b));
19 Console.Read();
20 }
21
22 }
23}
24
C#3.0使用Lambda表達式:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace ConsoleApplication3
7{
8 public delegate int Calculate(int a, int b);
9 class Program
10 {
11
12 static void Main(string[] args)
13 {
14 int a = 3;
15 int b = 4;
16 Calculate result = (ta, tb) => ta + tb;
17 Console.WriteLine(result(a,b));
18 Console.Read();
19 }
20
21 }
22}
23