Search code examples
c#asynchronousanonymous-methods

Anonymous methods and Async I/O


Can anyone tell me if I'm likely to run into unintended behavior if I use anonymous methods with Async I/O?

As an example:

Action<Socket> acceptedHandler = DoAccept
SocketAsyncEventArgs e = new SocketAsyncEventArgs();

e.Completed += ((sender, ea) => acceptedHandler(ea.AcceptSocket));

// Server is a Socket
if (!Server.AcceptAsync(e))
    acceptedHandler(e);

The same applies to BeginXXX/EndXXX async I/O.


Solution

  • From the code snippet you pasted it doesn't seem like there would be any issue. The only time to worry about anonymous methods is when you are capturing variables.