Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:21:33+00:00 2026-06-03T13:21:33+00:00

What is the scope of local variable declared in Linq Query. I was writing

  • 0

What is the scope of local variable declared in Linq Query.

I was writing following code

   static void Evaluate()
    {
        var listNumbers = Enumerable.Range(1, 10).Select(i => i);
        int i = 10;
    }

Compiler flagged error on line int i=10, stating

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 

I am unable to understand why this error is coming.

My understanding was that i will become out of scope after first line (in foreach loop). So i can be declared again.

Actual behavior is that i cannot be accessed after first line (in foreach loop), which is correct. But i cannot be declared again. This seems strange.

EDIT
This is a following question based on response by Andras. The answer is very good, but causes further doubts.

  static void Evaluate3()
    {
        var listNumbers = Enumerable.Range(1, 10).Select(i => i);
        var listNumbers1 = Enumerable.Range(1, 10).Select(i => i);
    }

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.

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.

Question, Either both Evaluate and Evaluate3 should not compile, or both should compile.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:21:34+00:00Added an answer on June 3, 2026 at 1:21 pm

    The key fact to note here is that a declaration:

    int i;
    

    …takes effect across the whole enclosing scope from start to finish – not just from the point at which it is declared. In .Net the declaration of a local variable is just an instruction to the compiler to reserve that name and local for the entire scope. That means once it’s declared, it’s already reserved for all lines before and after.

    In effect, it means that you should actually read Evaluate as:

    static void Evaluate()  
    {  
      int i;  
      var listNumbers = Enumerable.Range(1, 10).Select(i => i);  
      i = 10;
    } 
    

    And if you do write your method accordingly, you’ll see that the the compiler error occurs on the lambda declaration instead – which is perfectly reasonable. Thankfully, the C# compiler is clever enough, from a human perspective, to recognise that ordering of code is important to us, and it actually assigns the compiler error to whichever source line is the second or subsequent declaration; hence why in your version of Evaluate it happens on the line int i = 10;. With this knowledge of the actual lifetime of the function’s local i, the compiler is correct: the use of i there will conflict with the earlier use of i in the lambda.

    You can use explicit scoping to avoid this:

    static void Evaluate()  
    {
      var listNumbers = Enumerable.Range(1, 10).Select(i => i);
      {
        int i = 10;
      }
    }
    

    In the case of Evaluate3 you simply note that whilst both lambdas share the parent function scope, they also have their own, and it’s in there that their is are declared – and that’s why they don’t interfere with each other (they are, in effect, sibling scopes).

    Incidentally Evaluate and Evaluate3 can ultimately be simplified to this:

    static void Evaluate()
    {
      { 
        int i;
      }
      int i; //<-- error
    }
    
    static void Evaluate3()
    {
       { 
         int i;
       }
       { 
         int i;
       }
       //fine here - both i's are in different scopes.
    }
    

    And it’s actually the second scenario here that I’ve used explicit scoping for before – that is, in different scopes within the same function, where the i actually has a different type in each. Like I say – I’ve never done it again and the code in question is no longer live 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is the following code prints xxY? Shouldn't local variables live in the scope
The following code fails to compile stating A local variable named 'st' cannot be
I get the error : A local variable named 's' cannot be declared in
Consider the following C function. Does opening the braces to create a local scope
Possible Duplicate: Can a local variable's memory be accessed outside its scope? today i
Possible Duplicate: Can a local variable's memory be accessed outside its scope? #include <iostream>
I have declared a local variable named cont in a function named validate. I
If a function needs to modify a variable declared in global scope, it need
I have the following query var customers = from customer in context.tblAccounts join assoc
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.