码迷,mamicode.com
首页 > Windows程序 > 详细

C# Delegate

时间:2015-06-18 12:56:52      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

这里介绍一下C#的委托

委托的用处:1、方法参数化2、轮播

1、将方法参数化

public class Test
{
public delegate void TestEventHandler(string name);
public Test()
{
this.Start("run", this.Run);
}
private void Start(string name, TestEventHandler testEventHandler)
{
testEventHandler(name);
}
private void Run(string name)
{
Console.WriteLine(name);
}
}

2.轮播

public class Test
{
public delegate void TestEventHandler(string name);

public TestEventHandler testEventHandler;

public Test()
{
testEventHandler += Run1;
testEventHandler += Run2;
testEventHandler("run");
}
private void Run1(string name)
{
Console.WriteLine(name + "1");

}
private void Run2(string name)
{

Console.WriteLine(name + "2");
}
}

结果:

RUN1

RUN2

这就是轮播

C# Delegate

标签:

原文地址:http://www.cnblogs.com/hhxx123/p/4585446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!