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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:35:33+00:00 2026-05-10T20:35:33+00:00

The following code fails to compile stating A local variable named ‘st’ cannot be

  • 0

The following code fails to compile stating ‘A local variable named ‘st’ cannot be declared in this scope because it would give a different meaning to ‘st’, which is already used in a ‘child’ scope to denote something else’:

        var l = new List<string>();         l.Find(st => st.EndsWith('12'));         string st = 'why this fails?'; 

I understand why this won’t work:

        string preParent = '';         {             string preParent = 'Should fail cause we change the meaning';         } 

When we do the following we get ‘CS0103: The name ‘postParent’ does not exist in the current context’:

        {             string postParent=string.Empty;         }         postParent = 'Should this work?'; 

What I don’t get is why is the compiler smart enough to see that postParent is not within scope, but won’t let me define a new variable that has the same name as a variable used within a child scope (which is obviously out of scope at this point).

Is the compiler simple enforcing scope by refusing to let me use the variable? If so this makes sense.

===========

Edited:

I guess what I also find interesting is how you can have the same variable within two child scopes in a single method, so this is valid:

        {             string thisWorks= string.Empty;         }         {             string thisWorks= 'Should this work?';         } 

I’m just a little curious that you can have two variables with the same name as long as they are at the same level (if you look at scope as a tree). This makes sense because you can have local variables in two methods of the same class with the same name.

I’m just surprised that the compiler is able to differentiate and allow this, while it wouldn’t allow the postParent variable. And is this a technical limitation or was this a design decision? That’s what I’m really trying to get at;-)

  • 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. 2026-05-10T20:35:33+00:00Added an answer on May 10, 2026 at 8:35 pm

    Yes, the compiler is enforcing scope. Note that the scope of a variable is the lexical block it’s part of – not just from the point of declaration onwards, but the whole scope.

    The compiler is complaining because the assignment to postParent is outside its scope (which is only the nested braces). If you tried to declare a new variable at the point where you’re currently assigning to postParent the problem would be with the nested block, because the scope of postParent would include that nested block, even though it was before the declaration.

    Scopes are described in section 3.7 of the C# 3.0 specification.

    EDIT: To respond to your question edit.

    It’s just two simple rules:

    • you can’t declare a local variable when another local variable with the same name is in scope
    • the scope of a local variable is the block in which the declaration occurs

    I’m sure the language could have been designed such that the scope only began at the point of declaration, but I think it’s simpler (in terms of language complexity) to consider scopes as just blocks – so all local variables declared in the same block have the same scope, for example. That makes life a lot simpler when considering captured variables, too – as what gets captured depends on the scope, and nested scopes make life interesting…

    EDIT: The language spec has this to say about the original lambda expression example – it’s section 7.14.1:

    The optional anonymous-function-signature of an anonymous function defines the names and optionally the types of the formal parameters for the anonymous function. The scope of the parameters of the anonymous function is the anonymous-function-body. Together with the parameter list (if given), the anonymous-method-body constitutes a declaration space. For this reason, it is a compile-time error for the name of a parameter of the anonymous function to match the name of a local variable, local constant, or parameter whose scope includes the anonymous-method-expression or lambda-expression.

    Does that help?

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a big topic. The Spring reference doc devotes… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer Use default null. In SQL, null is very different from… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer Javascript runs in a single-thread, so if you have massive… May 11, 2026 at 11:35 pm

Related Questions

The following code fails to compile stating A local variable named 'st' cannot be
I get the following error message trying to run my page Compiler Error Message:
I'm attempting to create a concrete instance of the IAudioEvents COM interface (available in
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
This question is a follow up to: Why can’t I call a method outside

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.