using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
* C#委托
* 概念:将方法作为参数使用
* 作用:
* 委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数。(相当于C++的函数指针)
* 语法:
* 修饰符delegate<方法返回类型><委托名>(<方法参数>)
* public delegate void MyEventHandler();定义了一个委托MyEventHandler,他可以注册返回void类型无参方法
* 命名规范:EventHandler结尾
* 委托的实例:
* 委托类型 委托名;
* 委托类型 委托名 = new委托类型(方法);简便方式
* 委托 = 方法;对委托赋值
* 委托 += 方法 向委托上绑定方法
* 委托 -=方法 接触委托上绑定的方法
* C#事件
* 事件是类在发生其关注的事情时用来提供通知的一种方式。
* 事件发行者:一个事件的发行者(发送者),其实就是个对象,这个对象会自行维护本身的状态信息,当本身状态信息变动时,便出发一个事件,并通知说有的事件订阅者
* 事件订阅者:对事件感兴趣的对象,可以注册感兴趣的事件,在事件发行者出发一个事件后,会自动执行这段代码;
* 作用;
* 对委托进行管理,更符合代码的封装性
* 语法;
* public delegate void RunEventHandler();
* //在委托的机制下我们建立一个出版时间
* public event RunEventHandler OnRun;
* 命名规范除去EventHandler剩下的部分,首单词on
* OnRun += 方法 监听 add
* OnRun -= 方法 取消监听 remove
*
*/
namespace ConsoleApplication5
{
class Program
{
//定义无参无返回值委托
public delegate void BuyEventHandler();
//定义事件(一般与委托结合使用)
public static event BuyEventHandler OnBuy;
//有参无返回值委托
public delegate void HelloEventHandler(string name);
//无参有返回值委托
public delegate int AddAndSunEventHandler();
static void Main(string[] args)
{
XiaoMing();
Console.ReadKey();
}
public static void XiaoMing()
{
普通方法实现
//XiaoSongBuyWater();
//XiaoSongBuyBook();
XiaoSongBuyPen();
#region//无参无返回值委托
//委托直接用法
//BuyEventHandler buy = new BuyEventHandler(XiaoSongBuyWater);
第二种定义委托的方法
//BuyEventHandler buy;
//buy = XiaoSongBuyWater;
//buy += XiaoSongBuyBook;
//buy += XiaoSongBuyPen;
//buy();
//事件用法
OnBuy += XiaoSongBuyWater;//订阅
OnBuy += XiaoSongBuyBook;
OnBuy -= XiaoSongBuyWater;//取消订阅
if (OnBuy != null)
{
OnBuy();
}
#endregion
#region//无返回有参委托
HelloEventHandler hello = new HelloEventHandler(HelloChina);
hello += HelloEnglish;
hello("小强");
#endregion
#region //无参有返回值
AddAndSunEventHandler addAndsun = new AddAndSunEventHandler(Add);
addAndsun += Sun;
Console.WriteLine(addAndsun());
#endregion
}
//买水
public static void XiaoSongBuyWater()
{
Console.WriteLine("买一瓶水");
}
//买书
public static void XiaoSongBuyBook()
{
Console.WriteLine("买一本书");
}
//买笔
public static void XiaoSongBuyPen()
{
Console.WriteLine("买一支笔");
}
//中文打招呼
public static void HelloChina(string name)
{
Console.WriteLine("你好+" + name);
}
//英文打招呼
public static void HelloEnglish(string name)
{
Console.WriteLine("Good Morning+" + name);
}
public static int Sun()
{
return 1;
}
public static int Add()
{
return 2;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
* C#委托
* 概念:将方法作为参数使用
* 作用:
* 委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数。(相当于C++的函数指针)
* 语法:
* 修饰符delegate<方法返回类型><委托名>(<方法参数>)
* public delegate void MyEventHandler();定义了一个委托MyEventHandler,他可以注册返回void类型无参方法
* 命名规范:EventHandler结尾
* 委托的实例:
* 委托类型 委托名;
* 委托类型 委托名 = new委托类型(方法);简便方式
* 委托 = 方法;对委托赋值
* 委托 += 方法 向委托上绑定方法
* 委托 -=方法 接触委托上绑定的方法
* C#事件
* 事件是类在发生其关注的事情时用来提供通知的一种方式。
* 事件发行者:一个事件的发行者(发送者),其实就是个对象,这个对象会自行维护本身的状态信息,当本身状态信息变动时,便出发一个事件,并通知说有的事件订阅者
* 事件订阅者:对事件感兴趣的对象,可以注册感兴趣的事件,在事件发行者出发一个事件后,会自动执行这段代码;
* 作用;
* 对委托进行管理,更符合代码的封装性
* 语法;
* public delegate void RunEventHandler();
* //在委托的机制下我们建立一个出版时间
* public event RunEventHandler OnRun;
* 命名规范除去EventHandler剩下的部分,首单词on
* OnRun += 方法 监听 add
* OnRun -= 方法 取消监听 remove
*
*/
namespace ConsoleApplication5
{
class Program
{
//定义无参无返回值委托
public delegate void BuyEventHandler();
//定义事件(一般与委托结合使用)
public static event BuyEventHandler OnBuy;
//有参无返回值委托
public delegate void HelloEventHandler(string name);
//无参有返回值委托
public delegate int AddAndSunEventHandler();
static void Main(string[] args)
{
XiaoMing();
Console.ReadKey();
}
public static void XiaoMing()
{
普通方法实现
//XiaoSongBuyWater();
//XiaoSongBuyBook();
XiaoSongBuyPen();
#region//无参无返回值委托
//委托直接用法
//BuyEventHandler buy = new BuyEventHandler(XiaoSongBuyWater);
第二种定义委托的方法
//BuyEventHandler buy;
//buy = XiaoSongBuyWater;
//buy += XiaoSongBuyBook;
//buy += XiaoSongBuyPen;
//buy();
//事件用法
OnBuy += XiaoSongBuyWater;//订阅
OnBuy += XiaoSongBuyBook;
OnBuy -= XiaoSongBuyWater;//取消订阅
if (OnBuy != null)
{
OnBuy();
}
#endregion
#region//无返回有参委托
HelloEventHandler hello = new HelloEventHandler(HelloChina);
hello += HelloEnglish;
hello("小强");
#endregion
#region //无参有返回值
AddAndSunEventHandler addAndsun = new AddAndSunEventHandler(Add);
addAndsun += Sun;
Console.WriteLine(addAndsun());
#endregion
}
//买水
public static void XiaoSongBuyWater()
{
Console.WriteLine("买一瓶水");
}
//买书
public static void XiaoSongBuyBook()
{
Console.WriteLine("买一本书");
}
//买笔
public static void XiaoSongBuyPen()
{
Console.WriteLine("买一支笔");
}
//中文打招呼
public static void HelloChina(string name)
{
Console.WriteLine("你好+" + name);
}
//英文打招呼
public static void HelloEnglish(string name)
{
Console.WriteLine("Good Morning+" + name);
}
public static int Sun()
{
return 1;
}
public static int Add()
{
return 2;
}
}
}