1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace myTest
7 {
8 class Program
9 {
10 //定义一个委托类型,它包括0个参数,返回类型为void
11 delegate void DSimpleVoidFunc();
12
13 static void Main(string[] args)
14 {
15 DSimpleVoidFunc voidF; //定义DSimpleVoidFunc委托类型变量voidF
16 voidF = PrintHaHa; //为voidF赋值PrintHaHa函数
17 voidF(); //依次调用委托链中的函数,PrintHaHa
18 voidF += PrintHeHe; //将PrintHeHe添加到委托链
19 voidF(); //依次调用委托链中的函数,PrintHaHa->PrintHeHe
20 voidF -= PrintHeHe; //将PrintHeHe从委托链中移除
21 voidF.Invoke(); //依次调用委托链中的函数,PrintHaHa(与voidF()相同)
22 }
23
24 static void PrintHaHa()
25 {
26 System.Console.WriteLine("HaHa......");
27 }
28
29 static void PrintHeHe()
30 {
31 System.Console.WriteLine("HeHe......");
32 }
33 }
34 }
C#:什么是委托
时间: 2024-10-12 21:04:55