简体   繁体   中英

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)?

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

Its called Delegate Inference . The former lets the compiler infer the delegate type, the latter explicitly states it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM