第十二章 委托[《.net框架程序设计》读书笔记]
第十二章 委托一、 委托的使用
静态委托和实例委托,使用方法类似,这里给出一个使用可变参数委托的例子:
using System;
public class DelCls
{
public delegate void DelDef(params string[] strParams);
public static void CallDel(DelDef dd)
{
if(dd != null) //请务必在此处进行判断,这是个好习惯
{
dd("Hello", "world");
}
}
}
public class DelIns
{
//声明为private(私有)成员并不影响在类型内部使用委托
private static void ClsCallStr(params string[] strParams) //类型方法
{
//将字符串数组并顺序输出
foreach(string str in strParams)
{
Console.Write("{0} ", str);
}
Console.WriteLine();
}
public void InsCallStr(params string[] strParams) //实例方法
{
//将字符串数组并反序输出
for(int i = strParams.Length - 1; i >= 0; i --)
{
Console.Write("{0} ", strParams[i]);
}
Console.WriteLine();
}
public static void Main()
{
DelIns di = new DelIns();
DelCls.DelDef dd = null;
Console.WriteLine("combine two delegate:");
补充:asp.net教程,基础入门