Delegate: What is this? ----什麼是Delegate(委託)

爲什麼要寫這篇文章

首先,想找個藉口說英語很難,但是,我將盡可能的讓這篇文章通俗易懂。

其次,我想解釋一下我寫這篇文章的原因。一段時間以前,我和一個朋友討論過一個問題,他當時正在學習C#,他讓我給他解釋一下delegates和events。 當時,我非常地喫驚。 現在有那麼多的C#書籍,有用的鏈接,sample源代碼等等。但在討論的過程中,我意識到他已經讀過C#編程的書,瀏覽過很多有用的網絡資源,也研究過一些好的代碼實例, 但是對於理解delegates來說還是有不少的問題。

這個代碼實例就是我們討論的結果。這個例子非常地簡單,也可以說,非常地無聊。但是,他讓我的那位朋友理解了delegates的實質。我希望這個例子以及其中的註釋對於其他正在學習C#的朋友也有用。

正文開始

我建議你在閱讀這篇文章前先下載下來這個demo(演示)項目。是一個C#控制檯應用程序。解壓縮後,用.NET Studio打開那個.sln文件.

C# delegate 是一個callback函數。 換言之,delegate是class-server向class-client提供反饋的一種方式。

C# 的delegate要比一般的(標準的)callback函數更加的靈活,因爲它允許你定義一系列(strict list)可以從class-server端到class-client端的參數。

在我們的這個例子中,class-server有一個名字DataHolder。它包含了一個數據集合。而數據項在DataItem類中進行的定義,每一個數據都有兩個屬性:Name和Color。而DataHoder類包含一個允許遍歷查找數據集合以便找到符合搜索條件數據的方法。這個方法有兩個參數,第一個參數定義了搜索的條件,第二個參數定義了一個當符合搜索條件時可以調用的方法。

正確的理解class-server端遍歷所有的數據(from the start to the end)是非常重要的。如果數據項符合搜索條件,那麼class-server端就調用一個class-client端定義的方法。返回數目也就和符合搜索條件的那些數據項目數目相同。

MainClass即是class-client.第一句話就創建了一個DataHolder類的實例(1st),然後創建了CallBack類型逆光的delegate實例。這個delegate實例(procedure01) 包含了下面的信息:用戶自定義的class-server段要在class-client端調用的方法(PrintSearchResult),並且class-server必須把這些參數傳遞給這個用戶定義的參數.而傳遞的這個參數,是在delegate聲明中定義的。

以上就是C# delegate的主要思想。我建議你把這段代碼在編譯器中執行一遍看看他的執行結果。

===================================
部分代碼:

 

class DataItem
  {
   
private string m_ObjectName;
   
private string m_Color;
 
   
public DataItem(string Name, string Color)
   {
    m_ObjectName 
= Name;
    m_Color 
= Color;
  }

  
public string Name
  {
get {return m_ObjectName;}}
  
  
public string Color
  {
get {return m_Color;}}

 }
//Class Item

 
/**//*This is declaration of new data type "CallBack" which is based
  * on C# data type "delegate"
*/
 
public delegate void CallBack(string s);


 
/**//*This class represents collection of data items.*/
 
class DataHolder
 {
  
private ArrayList m_List;

  
public DataHolder()
  {
   m_List 
= new ArrayList();
   m_List.Add(
new DataItem("Desk""Black"));
   m_List.Add(
new DataItem("Car""Red"));
   m_List.Add(
new DataItem("Train""Yellow"));
   m_List.Add(
new DataItem("Tomato""Red"));
   m_List.Add(
new DataItem("Cheese""Yellow"));
   m_List.Add(
new DataItem("Computer","Grey"));
   m_List.Add(
new DataItem("Sausage""Red"));
   m_List.Add(
new DataItem("Cat""Black"));
  }

  
/**//* this method provides search functionality for caller. When an item 
   * meets search condition is found, client defined procedure activates.
*/
  
public void SearchData(string SearchCondition, CallBack ClientDefinedProcedure)
  {
   
foreach (DataItem i in this.m_List)
   {
    
if (i.Color == SearchCondition)
    {
     ClientDefinedProcedure(i.Name);
    }
   }
  }
 }
//Class DataHolder

 
class MainClass
 {
  [STAThread]
  
static void Main(string[] args)
  {
   
/**//*Create list of Data Items*/
   DataHolder lst 
= new DataHolder();

   
/**//* Cteate instance of delegate with type CallBack and bind it
    * to user defined procedure PrintSearchResults()
*/
   CallBack procedure01 
= new CallBack(PrintSearchResult);
   
   Console.WriteLine(
"Red color objects:");
   
/**//*Call search method with search critheria "Red" and user defined
    * procedure which is nesessary to execute when data item meets to search
    * creteria is found.
*/
   lst.SearchData(
"Red", procedure01);
   Console.WriteLine(
"---------------");
   Console.ReadLine();

  }

  
/**//*This procedure is called by DataHolder any time when data item meets to search
   * criteria is found.
*/
  
private static void PrintSearchResult(string s)
  {
   Console.WriteLine(s);
  }
 }
//MainClass
輸出的結果是
Red color objects:
Car
Tomato
Sausage
-------------
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章