I have this rather simple logic:
class Program
{
static void Main(string[] args)
{
using (TransactionScope ts = new TransactionScope())
{
System.Threading.Tasks.Parallel.Invoke(() =>
{
TransactionScope y = ts;
System.Diagnostics.Debug.WriteLine("Test");
},
() =>
{
System.Diagnostics.Debug.WriteLine("Test");
}
);
ts.Complete();
}
}
}
If you place breakpoints on the two Debug.WriteLine()
statements, you'll notice that when it breaks on the first, both y
and ts
are listed as locals by the debugger. But when it hits the breakpoint in the latter, ts
is not listed as a local and furthermore, adding ts
to the watch window gives The name 'ts' does not exist in the current context.
Is this variable capturing in action or is this some other mechanism? I've looked around at writeups on variable capture and I can't find anything that explicitly states that variables are only captured when they're used, but I'm making the assumption that it's called variable capture because it only "captures" what it needs and doesn't keep references to everything available.