附录牛人的委托和事件讲述: http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx
/*----------------------------------------------------------------------------------------------------------*/
想学习unity开发联网游戏了,在这里记录下学习C#网络编程的路线,闲话不多说。
/*----------------------------------------------------------------------------------------------------------*/
一.c#使用委托的一般步骤
1.定义委托关键字的使用delegate
delegate void SomeDelegate(type1 para1, type2 para2,...,typen paran);
2.声明委托
SomeDelegate sd;
3.实例化委托(其中obj是对象, InstanceMethod是他的实例化方法)
sd = new SomeDelegate(obj.InstanceMethod);
4.将委托用作某方法的参数
SomeMethod(sd);
5.在此方法中对委托进行调用(等同于调用与委托关联的方法)
private void SomeMethod(SomeDelegate someDelegate)
{
...
//使用委托
SomeDelegate(arg1, arg2,...,argn);
...
}
/*-------------------------------*/
在这里可能不懂。。后面举个例子
/*-------------------------------*/
二. c#使用委托的注意事项!
通过委托SomeDelegate实现对方法InstanceMethod的调用,必须有一个前提条件:
方法InstanceMethod有参数并且和SomeDelegate的参数一致(包括参数类型和参数),并且具有相同的放回置,此例为void。
//方法InstanceMethod的定义
private void InstanceMethod(type1 para1, type2 para2,...,typen paran)
{
//方法体
...
}
三.举个栗子!
1.用vs新建一个winform工程
2.搞两个进度条控件(progressBar) 一个button控件。
3.为button的点击时间编写一段代码,实现将两个进度条先后填充的效果,本实例只是为了学习委托,具体代码上不要在意。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _001_CSharp委托机制
{
public partial class fmMain : Form
{
public fmMain()
{
InitializeComponent();
}
/// <summary>
/// 使用一个简单的实例来搞委托,先不适用委托
/// 将两个进度条逐渐充满
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Click(object sender, EventArgs e)
{
/*搞两个for方便演示之后的委托*/
for (int i = 0; i <=100; i++)
{//进度条的最大值设置为100
//让线程每次循环等待50毫秒
Thread.Sleep(50);
pgProgressBar1.Value = i;
}
for (int i = 0; i <= 100; i++)
{
pgProgressBar2.Value = i;
}
}
}
}
4.使用委托实现相同的效果。贴代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _001_CSharp委托机制
{
public partial class fmMain : Form
{
//定义委托 本实例为了改变进度条的数值..
public delegate void SetProgressBarValueDelegate(int value);
//声明一个委托
SetProgressBarValueDelegate setProgressBarValue;
public fmMain()
{
InitializeComponent();
}
/// <summary>
/// 委托
/// 将两个进度条逐渐充满
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Click(object sender, EventArgs e)
{
setProgressBarValue = new SetProgressBarValueDelegate(SetProgressBarValue1);
SetProgressBarValueMethod(setProgressBarValue);
setProgressBarValue = new SetProgressBarValueDelegate(SetProgressBarValue2);
SetProgressBarValueMethod(setProgressBarValue);
}
private void SetProgressBarValueMethod(SetProgressBarValueDelegate setValue)
{
for (int i = 0; i <= 100; i++)
{
Application.DoEvents();
Thread.Sleep(50);
setValue(i);
}
}
private void SetProgressBarValue1(int value)
{
pgProgressBar1.Value = value;
}
private void SetProgressBarValue2(int value)
{
pgProgressBar2.Value = value;
}
}
}
四.总结
这个实例只是使用了委托的一个最为常规的方法,展示了使用委托的最基本的流程。
另外,委托可以视为一个自定义类,在使用的过程中他囊括了方法,即方法的方法。
在c#中的类型方法都是安全的,在多线程操作中,必须使用委托才能修改其他控件。