本篇主要讲解多播委托
1.多播委托的实例
public class MyDelegate { private delegate int NoParameterWithReturn();//1.声明委托 public static void Show() { NoParameterWithReturn method = new NoParameterWithReturn(ShowSomething);//2.委托的实例化 //多播委托 method += ShowSomething;//按顺序添加到方法列表 method += ShowSomething; method += ShowSomething; method += ShowSomething; method -= ShowSomething;//从方法列表的尾部去掉且只去掉一个完全匹配 Console.WriteLine("method.Invoke()结果是{0}", method.Invoke());//3.委托实例的调用 } private static int ShowSomething() { Console.WriteLine("这里是ShowSomething"); return 11; } } class Program { //多播委托 static void Main(string[] args) { Console.WriteLine("欢迎来到流星小子博客学习"); MyDelegate.Show(); Console.Read(); } }
2.运行结果
时间: 2024-10-13 01:13:46