附錄牛人的委託和事件講述: 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#中的類型方法都是安全的,在多線程操作中,必須使用委託才能修改其他控件。