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

  • Home
  • SEARCH
  • 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 541733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:22:24+00:00 2026-05-13T10:22:24+00:00

if(true) { string var = VAR; } string var = New VAR!; This will

  • 0
if(true)
{
    string var = "VAR";
}

string var = "New VAR!";

This will result in:

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

Nothing earth shattering really, but isn’t this just plain wrong? A fellow developer and I were wondering if the first declaration should be in a different scope, thus the second declaration cannot interfere with the first declaration.

Why is C# unable to differentiate between the two scopes? Should the first IF scope not be completely separate from the rest of the method?

I cannot call var from outside the if, so the error message is wrong, because the first var has no relevance in the second scope.

  • 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-05-13T10:22:25+00:00Added an answer on May 13, 2026 at 10:22 am

    The issue here is largely one of good practice and preventing against inadvertent mistakes. Admittedly, the C# compiler could theoretically be designed such that there is no conflict between scopes here. This would however be much effort for little gain, as I see it.

    Consider that if the declaration of var in the parent scope were before the if statement, there would be an unresolvable naming conflict. The compiler simply does not differentiate between the following two cases. Analysis is done purely based on scope, and not order of declaration/use, as you seem to be expecting.

    The theoretically acceptable (but still invalid as far as C# is concerned):

    if(true)
    {
        string var = "VAR";
    }
    
    string var = "New VAR!";
    

    and the unacceptable (since it would be hiding the parent variable):

    string var = "New VAR!";
    
    if(true)
    {
        string var = "VAR";
    }
    

    are both treated precisely the same in terms of variables and scopes.

    Now, is there any actual reason in this secenario why you can’t just give one of the variables a different name? I assume (hope) your actual variables aren’t called var, so I don’t really see this being a problem. If you’re still intent on reusing the same variable name, just put them in sibling scopes:

    if(true)
    {
        string var = "VAR";
    }
    
    {
        string var = "New VAR!";
    }
    

    This however, while valid to the compiler, can lead to some amount of confusion when reading the code, so I recommend against it in almost any case.

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

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • 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 The callback associated with on_fail is not supposed to return… May 13, 2026 at 3:11 pm
  • Editorial Team
    Editorial Team added an answer C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config is the one.. (on Windows 2003) May 13, 2026 at 3:11 pm
  • Editorial Team
    Editorial Team added an answer Try using query.values(Comment.comment). Note that it returns generator, not modified… May 13, 2026 at 3:11 pm

Related Questions

Without routing, HttpContext.Current.Session is there so I know that the StateServer is working. When
If the LINQ Count() extension method is invoked on an IEnumerable<T> that has a
I am trying to get some simple UDP communication working on my local network.
I have some data which I am formatting like this: // ... imagine populating
This was mentioned in my other question and I thought it might be useful

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.