目录
涉及知识点: delegate event 基础应用
一、概念
1、delegate 概念
delegate 一般定义在类的外部
添加的方法,要求:入参类型和返回值类型一致
Action -- 官方提供,无返回值泛型委托,最多设置16个参数
Func -- 官方提供,有返回值泛型委托,(返回值为最后一个参数)最多设置16个参数
多个委托一起调用时,返回值只可接收最后一个委托的返回值
2、event 概念
event 事件是delegate的安全版本
事件只可在定义的类中调用,在其他类中,只可添加或删除
三、delegate 委托案例
1、delegate 基础案例
代码编写:
//委托定义
public delegate void HolleDelegate();
internal class Program
{
static void Main(string[] args)
{
//委托创建
HolleDelegate holleDelegate = new HolleDelegate(Holle);
//委托添加
holleDelegate += World;
//委托调用
holleDelegate.Invoke();
Console.WriteLine("---------");
//委托删减
holleDelegate -= World;
//委托调用
holleDelegate.Invoke();
}
public static void Holle() => Console.WriteLine("你好啊");
public static void World() => Console.WriteLine("世界");
}
结果显示:

2、Action 无参案例
代码编写:
internal class Program
{
static void Main(string[] args)
{
//委托创建
Action holleDelegate = new Action(Holle);
//委托添加
holleDelegate += World;
//委托调用
holleDelegate.Invoke();
Console.WriteLine("--------------");
//委托删减
holleDelegate -= World;
//委托调用
holleDelegate.Invoke();
}
public static void Holle() => Console.WriteLine("你好啊");
public static void World() => Console.WriteLine("世界");
}
结果显示:

3、 Action 一个参数案例
代码编写:
internal class Program
{
static void Main(string[] args)
{
//委托创建
Action<int> holleDelegate = new Action<int>(Holle);
//委托添加
holleDelegate += World;
//委托调用
holleDelegate.Invoke(2);
Console.WriteLine("--------------");
//委托删减
holleDelegate -= World;
//委托调用
holleDelegate.Invoke(5);
}
public static void Holle(int id) => Console.WriteLine("你好啊");
public static void World(int id) => Console.WriteLine("世界");
}
结果显示:

4、 Func 一个参数和一个返回值案例
代码编写:
internal class Program
{
static void Main(string[] args)
{
int count = 0;
//委托创建
Func<string, int> holleDelegate = new Func<string, int>(Holle);
//委托添加
holleDelegate += World;
//委托调用
count = holleDelegate.Invoke("小王");
Console.WriteLine(count);
Console.WriteLine("--------------");
//委托删减
holleDelegate -= World;
//委托调用
count = holleDelegate.Invoke("小白");
Console.WriteLine(count);
}
public static int Holle(string name) => 2;
public static int World(string name) => 3;
}
结果显示:

四、 event 事件案例
代码编写:
//定义委托
public delegate void HolleDelegate();
internal class Program
{
static void Main(string[] args)
{
InvokeDefine invokeDefine = new InvokeDefine();
//事件添加
invokeDefine.HolleEvent += Student.Music;
invokeDefine.HolleEvent += Student.Math;
//事件调用
invokeDefine.Invoke();
Console.WriteLine("------------");
//事件删减
invokeDefine.HolleEvent -= Student.Math;
//事件调用
invokeDefine.Invoke();
}
}
public class InvokeDefine
{
public event HolleDelegate HolleEvent;
public void Invoke()
{
HolleEvent?.Invoke();
}
}
public class Student
{
public static void Music() => Console.WriteLine("我喜欢音乐");
public static void Math() => Console.WriteLine("我喜欢数学");
}
结果显示:

五、 委托异步调用
1、AsyncCallback 日志记录
Action<string> action = new Action<string>(DoSomeLongthing);
//实例化 异步回调函数
AsyncCallback asyncCallback = x => Console.WriteLine(x.AsyncState);
//开启异步调用,
//"XXXX" -- 函数入参
// asyncCallback -- 调用完成后,执行
//"调用完成" -- 传入 x.AsyncState中
action.BeginInvoke("XXXX", asyncCallback, "调用完成");
2、AsyncWaitHandle.WaitOne 阻塞
IAsyncResult asyncResult = action.BeginInvoke("begin", asyncCallback, "调用完成");
asyncResult.AsyncWaitHandle.WaitOne(); //一直等
//asyncResult.AsyncWaitHandle.WaitOne(-1); //一直等
//asyncResult.AsyncWaitHandle.WaitOne(10); //等10ms,超时进行下一步
Console.WriteLine("完成");
3、EndInvoke 获取异步结果
//加载一个有返回值的无参函数
Func<int> func = XX_method_XX;
//委托异步调用
IAsyncResult asyncResult = func.BeginInvoke(null, null);
//获取结果
int result = func.EndInvoke(asyncResult);
如有错误,烦请批评指正
本文详细介绍了C#中的委托(delegate)和事件(event)的基础应用,包括概念解析、委托实例(如Action和Func的使用)、事件案例以及异步调用的实现,涵盖了从基础到进阶的多个方面。
654

被折叠的 条评论
为什么被折叠?



