Consider this:
void StartUpdate(DataRequest dataRequest)
{
Task.Factory.StartNew(request => {... do something with "request" ...},
dataRequest);
}
Now, my question: can I use dataRequest inside the lambda expression, instead of passing it as second parameter to StartNew method? My concern is - that method will be executed on a different thread and I'm not sure if dataRequest would keep its state when used there.
Yes, you can.
This is called a Closure; it's a very powerful feature.
The thread-safety, or lack thereof, will be no different.
Whether you get the instance through the closure or through a StartNew
parameter, it's still the same object. (Unless it's a struct
, which would be indescribably evil)