local variable scope in linq anonymous method ( closure) <p>What is the scope of local variable declared in Linq Query.</p> <p>I was writing following code</p> <pre><code> static void Evaluate() { var listNumbers = Enumerable.Range(1, 10).Select(i =&gt; i); int i = 10; } </code></pre> <p>Compiler flagged error on line int i=10, stating </p> <pre><code>A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else </code></pre> <p>I am unable to understand why this error is coming.</p> <p>My understanding was that <code>i</code> will become out of scope after first line (in foreach loop). So <code>i</code> can be declared again. </p> <p>Actual behavior is that <code>i</code> cannot be accessed after first line (in foreach loop), which is correct. But <code>i</code> cannot be declared again. This seems strange.</p> <p>EDIT This is a following question based on response by Andras. The answer is very good, but causes further doubts.</p> <pre><code> static void Evaluate3() { var listNumbers = Enumerable.Range(1, 10).Select(i =&gt; i); var listNumbers1 = Enumerable.Range(1, 10).Select(i =&gt; i); } </code></pre> <p>Based on the logic of function Evaluate that .Select(i=>i), and int i=10, both i, are local to function block and hence complication error.</p> <p>Function Evaluate3 should not compile as well as there are two i in the method block, but it is compiling successfully without any warning/error. </p> <p>Question, Either both Evaluate and Evaluate3 should not compile, or both should compile.</p>