Delegates and Callbacks

后端 未结 4 957
有刺的猬
有刺的猬 2021-02-03 14:07

Does the term callback in the context of delegates mean ,\"a delegate delegating it works to another delegate inorder to finish some task\" ?

Example :(

4条回答
  •  醉酒成梦
    2021-02-03 14:24

    A callback is what the delegate executes when it is invoked. For example, when using the Asynchronous pattern using delegates, you would do something like this:

    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 is the callback, the code that is executed by invoking the delegate. See this Wikipedia article for some more information, or Google some more examples.

提交回复
热议问题