concept variable in category c#

This is an excerpt from Manning's book C# in Depth, Fourth Edition.
C# has been a statically typed language from the start: your code specifies the types of variables, parameters, values returned from methods, and so on. The more precisely you can specify the shape of the data your code accepts and returns, the more the compiler can help you avoid mistakes.
The expression used to initialize the variable must have a type.
Let’s start with a recap of what counts as a captured variable. Within a lambda expression, you can use any variable that you’d be able to use in regular code at that point. That could be a static field, an instance field (if you’re writing the lambda expression within an instance method[1]), the this variable, method parameters, or local variables. All of these are captured variables, because they’re variables declared outside the immediate context of the lambda expression. Compare that with parameters to the lambda expression or local variables declared within the lambda expression; those aren’t captured variables. The following listing shows a lambda expression that captures various variables. You’ll then look at how the compiler handles that code.
Listing 3.6. Capturing variables in a lambda expression
class CapturedVariablesDemo { private string instanceField = "instance field"; public Action<string> CreateAction(string methodParameter) { string methodLocal = "method local"; string uncaptured = "uncaptured local"; Action<string> action = lambdaParameter => { string lambdaLocal = "lambda local"; Console.WriteLine("Instance field: {0}", instanceField); Console.WriteLine("Method parameter: {0}", methodParameter); Console.WriteLine("Method local: {0}", methodLocal); Console.WriteLine("Lambda parameter: {0}", lambdaParameter); Console.WriteLine("Lambda local: {0}", lambdaLocal); }; methodLocal = "modified method local"; return action; } } In other code var demo = new CapturedVariablesDemo(); Action<string> action = demo.CreateAction("method argument"); action("lambda argument"); !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":13}]]"} !@%STYLE%@!Lots of variables are involved here:

This is an excerpt from Manning's book C# in Depth.
There’s a lot of fluff involved in creating the properties and variables—code that complicates the simple task of encapsulating a string and a decimal.
When a variable is captured, it really is the variable that’s captured by the anonymous method, not its value at the time the delegate instance was created. You’ll see later that this has far-reaching consequences, but first you need to understand what that means for a relatively straightforward situation.
The following listing has a captured variable and an anonymous method that both prints out and changes the variable. You’ll see that changes to the variable from outside the anonymous method are visible within the anonymous method, and vice versa.
Listing 5.11. Accessing a variable both inside and outside an anonymous method
string captured = "before x is created"; MethodInvoker x = delegate { Console.WriteLine(captured); captured = "changed by x"; }; captured = "directly before x is invoked"; x(); Console.WriteLine(captured); captured = "before second invocation"; x();
In the good old days, it was reasonable to say that pieces of code like this were semantically identical. Indeed, they’d usually compile to the same IL—and they still will, if there aren’t any anonymous methods involved. All the space for local variables is allocated on the stack at the start of the method, so there’s no cost to redeclaring the variable for each iteration of the loop.[8] In our new terminology, the single variable will be instantiated only once, but the multiple variable will be instantiated 10 times—it’s as if there were 10 local variables, all called multiple, which were created one after another.
8 In my view, it’s also cleaner to redeclare the variable unless you explicitly need to maintain its value between iterations.
I’m sure you can see where I’m going—when a variable is captured, it’s the relevant “instance” of the variable that’s captured. If you captured multiple inside the loop, the variable captured in the first iteration would be different from the variable captured the second time round, and so on. The following listing shows exactly this effect.
Listing 5.13. Capturing multiple variable instantiations with multiple delegates
List<MethodInvoker> list = new List<MethodInvoker>(); for (int index = 0; index < 5; index++) { int counter = index * 10; #1 list.Add(delegate { Console.WriteLine(counter); #2 counter++; }); } foreach (MethodInvoker t in list) { t(); #3 } list[0](); #4 list[0](); list[0](); list[1](); #5 #1 – 1 Instantiates counter #2 – 2 Prints and increments captured variable #3 – 3 Executes all five delegate instances #4 – 4 Executes first one three more times #5 – 5 Executes second one again !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!
Before capturing a variable declared by a for or foreach statement, consider whether your delegate is going to live beyond the loop iteration, and whether you want it to see the subsequent values of that variable. If not, create another variable inside the loop that just copies the value you do want. (In C# 5 you don’t need to worry about foreach statements, but you still need to take care in for statements.) If you create multiple delegate instances (whether in a loop or explicitly) that capture variables, put thought into whether you want them to capture the same variable. If you capture a variable that doesn’t actually change (either in the anonymous method or the enclosing method body), you don’t need to worry as much.