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 7944315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:30:59+00:00 2026-06-04T00:30:59+00:00

Why does my parameter x behave so erratically? Example 1 – Doesn’t exist in

  • 0

Why does my parameter x behave so erratically?

  1. Example 1 – Doesn’t exist in the current context.
  2. Example 2 – Cannot reuse x because it’s defined in a ‘child’ scope.
  3. Example 3 – Fine. This is the part where I am confused. Perhaps a different ‘child’ scope?

Example 1:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
var result = list.Where(x => x < 3);
Console.Write(result.ElementAt(x));

creates this compile time error:

The name ‘x’ does not exist in the current context

which I expect.

Example 2:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
var result = list.Where(x => x < 3);
int x = 1;
Console.Write(result.ElementAt(x));

produces this compile-time error:

A local variable named ‘x’ cannot be declared in this scope because it
would give a different meaning to ‘x’, which is already used in a
‘child’ scope to denote something else

I understand the scoping as answered in this question, Is there a reason for C#'s reuse of the variable in a foreach?. However, this is something I’ve never seen before. In addition, it makes the answers to this question, What is the scope of a lambda variable in C#?, incomplete or wrong.

Example 3:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
List<string> stringList = new List<string> { "A", "B" };
var result = list.Where(x => x < 3);
var result2 = stringList.Where(x => x != "A");

Console.Write(result2);

No errors produced.


With the accepted answer, these blog posts from Eric Lippert helped me wrap my head around what was happening. If anyone is still confused:

declaration space

simple names

  • 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-04T00:31:00+00:00Added an answer on June 4, 2026 at 12:31 am

    In Example 1, x is defined in the local scope of the lamdba expression and is not visible to the third line

    In Example 2, now you’ve declared two variables named “x” at the same declaration scope (visibility is different)

    With a lambda or anonymous method, it “captures” the scope at which it is running. If you have a local x in the same scope as the lambda definition, then it “captures” that x to pull into what the lambda can access–thus resulting in two definitions of “x”. What you declare in the lambda doesn’t get captured in the other direction so it isn’t visible outside the lambda.

    In Example 3, Now you’re not using a variable that is local only the lambda outside the lambda, and not naming something the same at the same declaration scope.

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

Sidebar

Related Questions

If it is possible, how does the parameter need to look like in the
We're getting a server error saying Parameter count does not match Parameter Value count.
What does the -f flag mean in the fifth parameter in the PHP mail
How does C/C++ deal if you pass an int as a parameter into a
When you have a function with a string parameter, does that create another instance
Why this parameter does not pass: file.php?t=%26quot%3B%26gt%3B%0A%09%09%09%26lt Instead it gives: Forbidden You don't have
I have this doubt, what parameter does control timeouts for JSESSIONID?. Specifically consider this
In the following situation, how does the parameter-list for sortMyVectors have to look like,
PHP's assert statement doesn't behave like most other languages. assert('return false'); actually evaluates the
I have a stored procedure that does some parameter validation and should fail and

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.