Dolphin
Dolphin

Reputation: 38763

Why lambda expression has no return value?

I read a code to send a email,here part of it:

MailUserState state = new MailUserState()
{
    AutoReleaseSmtp = m_autoDisposeSmtp,
    CurMailMessage = mMailMessage,
    CurSmtpClient = m_SmtpClient,
    IsSmpleMail = true,
    UserState = AsycUserState,
};
if (m_autoDisposeSmtp)
  m_SmtpClient = null;

ThreadPool.QueueUserWorkItem((userState) =>
{
    MailUserState curUserState = userState as MailUserState;
    curUserState.CurSmtpClient.SendAsync(mMailMessage, userState);
}, state);

Why the lambda expression has no return value?

I think it shoud return a callback instance object.But it has no return statement.Why?

Upvotes: 0

Views: 839

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Why the lambda expression has no return value?

The first argument of the ThreadPool.QueueUserWorkItem method is a WaitCallback delegate which looks like this:

public delegate void WaitCallback(object state)

So as you can see it is a function taking one object parameter and having no return value. And that's exactly what the lambda expression in your code is. The QueueUserWorkItem method will draw a thread from the thread pool if/when available and it will execute the code in the callback on this thread. There is no return value.

Upvotes: 7

Related Questions