自定义委托,事件,参数的简单随笔
.Net Framework的编码规范:
- 委托类型的名称都应该以EventHandler结束。
- 委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。
- 继承自EventArgs的类型应该以EventArgs结尾。
event本身是C#在委托基础上封装一些用于多事件注册的机制,这是event和委托的区别,委托仅仅是控件类定义和用户自定义事件方法共同遵守的方法的contract契约,一个方法类型而已 。
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateApplication
{
class Program
{
static void Main(string[] args)
{
string str = "show !!!";
InitEvent(str);
Console.ReadLine();
}
private static void InitEvent(string showStr)
{
//初始化事件参数类,赋值
InformationEventArgs eventArgs = new InformationEventArgs();
eventArgs.Information = showStr;
InformationEvent eventer = new InformationEvent();
InformationCall call = new InformationCall();
eventer.InformationClick+= new InformationHandler(call.ShowInformation);
eventer.OnInformation(eventArgs);
Console.ReadLine();
}
}
/// <summary>
/// 事件参数
/// </summary>
public class InformationEventArgs : EventArgs
{
private string _message;
public string Information
{
get
{
return _message;
}
set
{
_message = value;
}
}
}
//委托
public delegate void InformationHandler(object sender,InformationEventArgs e);
//事件
public class InformationEvent
{
public event InformationHandler InformationClick;
//触发事件
public void OnInformation(InformationEventArgs e)
{
if (InformationClick != null)
{
InformationClick(this, e);//匹配委托 进行InformationEventArgs传值
}
}
}
public class InformationCall
{
private string _strInformation;
public string InformationString
{
get
{
return _strInformation;
}
set
{
_strInformation = value;
}
}
//调用的方法 匹配委托 显示事件参数的值
public void ShowInformation(object sender, InformationEventArgs e)
{
InformationString = e.Information;
Console.WriteLine(InformationString);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace DelegateApplication
{
class Program
{
static void Main(string[] args)
{
string str = "show !!!";
InitEvent(str);
Console.ReadLine();
}
private static void InitEvent(string showStr)
{
//初始化事件参数类,赋值
InformationEventArgs eventArgs = new InformationEventArgs();
eventArgs.Information = showStr;
InformationEvent eventer = new InformationEvent();
InformationCall call = new InformationCall();
eventer.InformationClick+= new InformationHandler(call.ShowInformation);
eventer.OnInformation(eventArgs);
Console.ReadLine();
}
}
/// <summary>
/// 事件参数
/// </summary>
public class InformationEventArgs : EventArgs
{
private string _message;
public string Information
{
get
{
return _message;
}
set
{
_message = value;
}
}
}
//委托
public delegate void InformationHandler(object sender,InformationEventArgs e);
//事件
public class InformationEvent
{
public event InformationHandler InformationClick;
//触发事件
public void OnInformation(InformationEventArgs e)
{
if (InformationClick != null)
{
InformationClick(this, e);//匹配委托 进行InformationEventArgs传值
}
}
}
public class InformationCall
{
private string _strInformation;
public string InformationString
{
get
{
return _strInformation;
}
set
{
_strInformation = value;
}
}
//调用的方法 匹配委托 显示事件参数的值
public void ShowInformation(object sender, InformationEventArgs e)
{
InformationString = e.Information;
Console.WriteLine(InformationString);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架