Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this error "local variable cannot be declared in this scope because it ... is already used"?
    primarykey
    data
    text
    <p>I am trying to run the code from the blog article by <a href="http://blogs.msdn.com/b/ericlippert/archive/2006/08/18/why-does-a-recursive-lambda-cause-a-definite-assignment-error.aspx" rel="nofollow">Eric Lippert "Why does a recursive lambda cause a definite assignment error?"</a> </p> <p>but instead of running (or giving the compilation "definite assignment error" I am getting: </p> <blockquote> <p>A local variable named 't' cannot be declared in this scope because it would give a different meaning to 't', which is already used in a 'parent or current' scope to denote something else </p> </blockquote> <p>Why?<br> Where is it already used in parent or current scope?<br> Tried to rename it having gotten the same error<br> How should I change the code to launch this code? </p> <pre><code>using System; namespace LippertRecursiveLambdaError { class Program { static void Main(string[] args) { Tree&lt;int&gt; tree = new Tree&lt;int&gt; (3, new Tree&lt;int&gt; (4, new Tree&lt;int&gt;(1), null), new Tree&lt;int&gt;(5)); PrintTree(tree); Console.ReadLine(); } delegate void Action&lt;A&gt;(A a); delegate void Traversal&lt;T&gt;(Tree&lt;T&gt; t, Action&lt;T&gt; a); static void DepthFirstTraversal&lt;T&gt;(Tree&lt;T&gt; t, Action&lt;T&gt; a) { if (t == null) return; a(t.Value); DepthFirstTraversal(t.Left, a); DepthFirstTraversal(t.Right, a); } static void Traverse&lt;T&gt;(Tree&lt;T&gt; t, Traversal&lt;T&gt; f, Action&lt;T&gt; a) { f(t, a); } static void Print&lt;T&gt;(T t) { System.Console.WriteLine(t.ToString()); } /*static void PrintTree&lt;T&gt;(Tree&lt;T&gt; t) { Traverse(t, DepthFirstTraversal, Print); }*/ static void PrintTree&lt;T&gt;(Tree&lt;T&gt; t) { //Traversal&lt;T&gt; df = (t, a)=&gt; ************** Traversal&lt;T&gt; df = null; //======================================== //The next line gives compilation error //A local variable named 't' cannot be declared in this scope //because it would give a different meaning to 't', //which is already used in a 'parent or current' scope to denote something else df = (t, a) =&gt; { if (t == null) return; a(t.Value); df(t.Left, a); df(t.Right, a); }; Traverse(t, df, Print); }//PrintTree }//class class Tree&lt;T&gt; { public Tree&lt;T&gt; Left; public Tree&lt;T&gt; Right; public T Value; public Tree(T value) { Value = value; } public Tree(T value, Tree&lt;T&gt; left, Tree&lt;T&gt; right) { Value = value; Left = left; Right = right; } } }//namespace </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload