多播委托的实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void hello()
{
Console.WriteLine("hello");
}
static void world()
{
Console.WriteLine("world!");
}
static void Main(string[] args)
{
Action t = hello;//action类用来封装方法
t += world;//使t指向hello和world两个方法
t -= world;//使t不指向world方法
t -= hello;//使t不指向hello方法,或者说hello方法脱离委托掌控
t();//没有方法的委托,一般会报出未将对象引用到实例的错误
Console.ReadKey();
}
}
}
多播委托只能得到调用方法的最后一个结果,所以想要调用全部的方法,一般我们把多播委托的返回值设为void
throw new exception
在hello或world中抛出以上异常,就会发现,一旦一个方法出现异常,整个迭代直接结束。
当你背单词时,阿拉斯加的鳕鱼正跃出水面。 当你解微分方程时,大洋彼岸的海鸥正拂过费城。 当你晚自习时,极图的夜空散满了五彩斑斓。 当你为自己的未来踏踏实实努力时, 那些你从未见过的风景, 那些你以为不会遇到的人, 你要的一切, 正一步步向你走来。
浙公网安备 33010602011771号