2009-11-17 81 views
6

代表的上下文中的術語回調是否意味着「代表委託它可以工作給另一個代表以完成某個任務」?代表和回調

實例:(根據我的理解,我實現了一個回調,糾正我,如果它是錯誤

namespace Test 
{ 
    public delegate string CallbackDemo(string str); 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      CallbackDemo handler = new CallbackDemo(StrAnother); 
      string substr = Strfunc(handler); 
      Console.WriteLine(substr); 
      Console.ReadKey(true); 
     } 

     static string Strfunc(CallbackDemo callback) 
     { 
      return callback("Hello World"); 
     } 

     static string StrAnother(string str) 
     { 
      return str.Substring(1, 3).ToString(); 
     } 
    } 
} 

請舉例是必要的。

+1

imho有價值的認爲委託作爲一個類型,是一個函數指針的方法:像一個接口或類:它是一個實例的模板,它本身是其內部的引用方法:實例可以傳遞在對象周圍並隨時調用/執行。 如果您有任何方法可以通過Jon Skeet掌握「C#深度」,並閱讀關於代表的第2章節以及關於代表的第5章,您將瞭解代表從他們的「幼蟲」的「進化」階段「在C#1.0中,到C#3.0中完全」演變「的形式。我從來沒有找到更好的展覽 – BillW 2009-11-17 04:08:52

回答

11

你的例子是一個好的開始,但它是不正確的。您不會在該方法中創建新的委託,它將用於該類中事件的聲明。看到你的代碼的這個修改後的例子:

namespace Test 
{ 
    //In this case, this delegate declaration is like specifying a specific kind of function that must be used with events. 
    public delegate string CallbackDemo(string str); 
    class Program 
    { 
     public static event CallbackDemo OnFoobared; 
     static void Main(string[] args) 
     { 
      //this means that StrAnother is "subscribing" to the event, in other words it gets called when the event is fired 
      OnFoobared += StrAnother; 
      string substr = Strfunc(); 
      Console.WriteLine(substr); 
      Console.ReadKey(true); 
      //this is the other use of delegates, in this case they are being used as an "anonymous function". 
      //This one takes no parameters and returns void, and it's equivalent to the function declaration 
      //'void myMethod() { Console.WriteLine("another use of a delegate"); }' 
      Action myCode = delegate 
      { 
       Console.WriteLine("another use of a delegate"); 
      }; 
      myCode(); 
      Console.ReadKey(true); 
      //the previous 4 lines are equivalent to the following however this is generally what you should use if you can 
      //its called a lambda expression but it's basically a way to toss arbitrary code around 
      //read more at http://www.developer.com/net/csharp/article.php/3598381/The-New-Lambda-Expressions-Feature-in-C-30.htm or 
      //http://stackoverflow.com/questions/167343/c-lambda-expression-why-should-i-use-this 
      Action myCode2 =() => Console.WriteLine("a lambda expression"); 
      myCode2(); 
      Console.ReadKey(true); 
     } 

     static string Strfunc() 
     { 
      return OnFoobared("a use of a delegate (with an event)"); 
     } 
     static string StrAnother(string str) 
     { 
      return str.Substring(1, 3).ToString(); 
     } 
    } 
} 

我只是在這裏劃傷了表面;搜索堆棧溢出的「委託C#」和「lambda表達式C#」爲更多!

+0

非常感謝你 – user160677 2009-11-17 07:06:20

+0

不客氣! – RCIX 2009-11-17 08:04:54

3

回調基本上是一個委託給一個過程,該過程將在某個適當的位置「回調」。例如,在諸如WebRequest.BeginGetResponse或WCF BeginXxx操作的異步調用中,您將傳遞一個AsyncCallback。工作人員將「回叫」任何你以AsyncCallback傳入的方法,在這種情況下,當它完成時,讓你知道它已完成並獲得結果。

事件處理程序可以被認爲是另一個例子。例如,當您將處理程序附加到Click事件時,該按鈕將在發生點擊時「回調」該處理程序。

3

回調是委託在調用時執行的內容。例如,使用使用委託的異步模式的時候,你會做這樣的事情:

public static void Main(string[] args) 
{ 
    Socket s = new Socket(...); 

    byte[] buffer = new byte[10]; 
    s.BeginReceive(buffer, 0, 10, SocketFlags.None, new AsyncCallback(OnMessageReceived), buffer); 

    Console.ReadKey(); 
} 

public static void OnMessageReceived(IAsyncResult result) 
{ 
    // ... 
} 

OnMessageReceived是回調,也就是通過調用委託執行的代碼。有關更多信息,請參見this Wikipedia article,或參閱Google更多示例。

1

這會因爲正確的原因而被投票。 C#沒有實現代理,它實現的是呼叫轉移。這種命名的不正確使用可能是C#在這方面最大的問題。

下面的ACM論文是後面稱爲代表的第一個描述。基本上,委託是看起來是對象實例的東西(實際上它是如何實現的並不重要)。這意味着您可以從委託中調用方法,訪問屬性等。

http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.html

什麼C#實現IS回調或呼叫轉移(全部依賴於你如何使用它們)。這些不是代表。爲了成爲代表,可以像對象本身一樣訪問該對象。

當一支筆將一個繪圖信息委託給一個原型筆時,它會說「我不知道如何處理繪圖信息。如果可以的話,我希望你給我回答,但是如果你還有其他問題,比如我的x變量的價值,或者需要做什麼,你應該回來問我。「如果消息被進一步委託,關於變量值或回覆消息的請求的所有問題都被推斷給首先委託消息的對象。 - 亨利·利伯曼

那麼,它是如何搞砸了?說實話,我不知道。我知道在C#之前很久以來我一直在使用代表(已超過16年),而C#實現的代碼並非代表。

這裏有一個很好的解釋。

http://www.saturnflyer.com/blog/jim/2012/07/06/the-gang-of-four-is-wrong-and-you-dont-understand-delegation/

真正的代表團比創建回調或呼叫轉移更多。一個真正的委託可以讓我調用該對象的任何方法,獲取和/或設置公共屬性等 - 就好像它是對象本身一樣。這比C#「委託」更加強大,實際上更易於使用。

的OP問:

是否在與會代表的上下文中的術語回調的意思是,「一個代表委託其運作的又委託序來完成一些任務」?

答案是肯定的。在代表的上下文中回撥可以只能用來完成某些任務。例如,你有一個從氣象站點獲取數據的類。由於它是非確定性的,所以當數據被接收(也許被解析)時實現回調將會很快。

至於爲什麼代表團被破壞,我不知道。我期待C#實現TRUE委派。

+0

因爲......而投了票? – 2015-04-09 14:09:30