# 委托类型 事件触发 回调函数 按钮事件
DelegateButton\DelegateButton.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
DelegateButton\Program.cs
public delegate void ButtonClickDelegate(object sender, EventArgs e);
public class Button
{
public event ButtonClickDelegate OnClick;
public void Click()
{
OnClick?.Invoke(this, EventArgs.Empty);
}
}
class Program
{
static void Main(string[] args)
{
Button button = new Button();
button.OnClick += Button_Click;
button.Click();
}
private static void Button_Click(object sender, EventArgs e)
{
Console.WriteLine("Button was clicked!");
}
}