C # / Lambda: querying an object parameter parameter (noob)

A completely new question here with a remark of this post: ThreadPool.QueueUserWorkItem with lambda expression and anonymous method

Specifically, this:

ThreadPool.QueueUserWorkItem(
    o => test.DoWork(s1, s2)
    );

Can someone explain what “o” is? I see (in VS2008) that this is an object parameter, but I basically don’t understand why and how.

+3
source share
5 answers

ThreadPool.QueueUserWorkItemdelegate required WaitCallbackas an argument.

This delegate type corresponds to the function of a voidsingle type argument Object.

So the full version of the call can be

ThreadPool.QueueUserWorkItem(
    new WaitCallback(delegate(object state) { test.DoWork(s1,s2); });
);

Will be more concise

ThreadPool.QueueUserWorkItem(
    delegate(object state) { test.DoWork(s1,s2); };
);

Using C # 3.0 syntax, we can write it in shorter form:

ThreadPool.QueueUserWorkItem(
    (object state) => { test.DoWork(s1,s2); };
);

# 3.0 - state. , .

+10

QueueUserWorkItem, WaitCallback :

public delegate void WaitCallback(
    Object state
)

:

 Type: System.Object
 An object containing information to be used by the callback method.

, QueueUserWorkItem - , ( ) , void. o - . , .

+1

: o - , QueueUserWorkItem. , null.

, -.

0

ois a formal parameter of a lambda function. Its type is determined by the type of the QueueUserWorkItem parameter.

0
source

The other answers are good, but maybe this helps if you see what is equivalent without using either lambda expressions or delegation methods (i.e. this is the .NET 1 path):

void SomeMethod()
{
  //...
  ThreadPool.QueueUserWorkItem(new WaitCallback(TheCallback));
  //...
}

void TheCallback(object o)
{
  test.DoWork(s1, s2);
}
0
source

Source: https://habr.com/ru/post/1723605/


All Articles