Search code examples
c#network-programmingasyncsocket

What is different Callback vs new AsyncCallback(Callback)?


For AsyncSocket

// accept
...
listener.BeginAccept( new AsyncCallback(AcceptCallback), listener);
// listener.BeginAccept( AcceptCallback, listener);
...

public void AcceptCallback(IAsyncResult ar)
{
...
}

// recieve
...
socket.BeginReceive(buffer, offset, length, 0, new AsyncCallback(ReadCallback), asyncSocket);
// socket.BeginReceive(buffer, offset, length, 0, ReadCallback, asyncSocket);
...

public void ReadCallback(IAsyncResult ar)
{
...
}

We can use just Callback instead new AsyncCallback(Callback)

What is different just Callback vs new AsyncCallback(Callback)?


Solution

  • They're the same. The "new" keyword was needed in older versions of the compiler. The newer version of the compiler can infer the delegate. The same code is generated either way.