Delegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateAndEvent
{
public delegate void nDelegate();
public delegate void sDelegate(string str);
public delegate void mulDelegate(string str);
class Program
{
public delegate void eventDelegate(string str);
public event eventDelegate log;
protected void Onlog(string message)
{
if (log != null)
{
log(message);
}
}
public void Process()
{
OnLog("Process() begin");
OnLog("Process() end");
}
protected void OnLog(string message)
{
if (log != null)
{
log(message);
}
}
public static void Print()
{
Console.WriteLine("Called by delegate");
Console.ReadKey();
}
public static void prints(sDelegate sdele)
{
if (sdele != null)
{
sdele("Called by delegate!");
}
}
public static void prints(mulDelegate mdele)
{
if (mdele != null)
{
mdele("Called by delegate!");
}
}
static void Logger(string s)
{
Console.WriteLine(s);
}
static void getString(string ss)
{
Console.WriteLine(ss);
}
static void Main(string[] args)
{
Program p = new Program();
p.log += Logger;
p.log += getString;
p.Process();
sDelegate sdelegate = new sDelegate(Logger);
prints(sdelegate);
mulDelegate mdelegate = null;
mdelegate += Logger;
mdelegate += getString;
prints(mdelegate);
nDelegate mDelegate = new nDelegate(Print);
mDelegate();
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步