Hi, I have a simple question about .net delegates. Say I have something like this:
public void Invoke(Action<T> action)
{
Invoke(() => action(this.Value));
}
public void Invoke(Action action)
{
m_TaskQueue.Enqueue(action);
}
The first function encloses a reference to this.Value
. During runtime, when the first, method with generic parameter gets called, it will provide this.Value
somehow to the second one, but how? These came into my mind:
- Call by value (struct) - the current value of
this.Value
gets passed, so if them_TaskQueue
executes it 5 minutes later, the value will not be in it's recent state, it will be whatever it was when first referencing. - Call by reference (reference type) - then the most recent state of
Value
will be referenced during execution of action but if I changethis.Value
to another reference before execution of action, it will still be pointing to the old reference - Call by name (both) - where
this.Value
will be evaluated when the action gets called. I believe the actual implementation would be holding a reference tothis
then evaluateValue
on that during actual execution of delegate since there is no call by name.
I assume it would be Call by name style but could not find any documentation so wondering if it is a well defined behavior. This class is something like an Actor in Scala or Erlang so I need it to be thread safe. I do not want Invoke
function to dereference Value
immediately, that will be done in a safe thread for this
object by m_TaskQueue
.
Thanks.