I am using ThreadPool with the follwoing code:-
ThreadPool.QueueUserWorkItem
(o =>
MyFunction()
);
I am not sure what does o=> does in this code. Can anyone help me out.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It describes a lambda (anonymous) function. In this case it’s a function that takes one argument, o, and then executes MyFunction (although in this case it’s basically throwing the value of o away). It’s equivalent to:
The type of o is inferred based on whatever QueueUserWorkItem expects. QueueUserWorkItem expects type WaitCallback so in this case o should be of type object because WaitCallback is delegate for methods with one parameter of type object that return void.
As for the meaning of this particular code fragment; you’re basically adding a function (work item) to a queue that will be executed by one of the threads in the pool (when it becomes available). That particular code fragment just describes a nice, succinct way of passing in the function without having to go through the trouble of fully defining a class method.
Incidentally, I, and others, tend to read => as ‘such that’. Some people read it as ‘goes to’.