C#中eventhandler
时间: 2024-02-05 16:08:26 浏览: 50
在C#中,EventHandler是一个委托类型,用于处理事件。它是一个泛型委托,可以接受不同类型的参数。通常,EventHandler用于定义事件的处理程序。
以下是两个关于C#中EventHandler的例子:
1. 使用字符串作为参数的EventHandler:
```csharp
public class Headmaster
{
public event EventHandler<string> ActHandler;
public void Come()
{
Console.WriteLine("校长来了");
ActHandler?.Invoke(null, "王校长");
}
}
```
2. 使用自定义参数类作为参数的EventHandler:
```csharp
public class HeadArgs : EventArgs
{
public string Name { get; set; }
public int MedalCount { get; set; }
}
public class Headmaster
{
public event EventHandler ActHandler;
public void Come()
{
Console.WriteLine("校长来了");
HeadArgs args = new HeadArgs() { Name = "王校长", MedalCount = 10 };
ActHandler?.Invoke(null, args);
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)