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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:07:00+00:00 2026-05-31T10:07:00+00:00

I’m just couris about whats happning behind the scenes. I have this code and

  • 0

I’m just couris about whats happning behind the scenes. I have this code and of course it will not compile cause I create the hello variable inside a if statement and later try to declare it agian. Why dosen’t .NET allow me to do so? Whats happning behind the scences that could make the hello variable interfeer with the one inside the statement.

It’s very stright forward why this could interfeer if the variable was declared before the if statement.

    public void Test() {
        if (true)
        {
            var hello = "";
        }
        var hello = "";

        Console.Write(hello);
    }
  • 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-31T10:07:01+00:00Added an answer on May 31, 2026 at 10:07 am

    Just to clarify, there are two rules violated here.

    The first is that nested local variable declaration spaces may not contain two declarations of the same name.

    The second is that nested local scopes may not contain two usages of the same simple name or declaration to mean two different things.

    Both rules are violated. Note that the first rule is essentially a special case of the second rule. The second rule is more general; for example, this is also a violation:

    class C
    {
        int x;
        void M()
        {
            if (whatever)
            {
                string x = "";
            }
            x = 10;
        }
    }
    

    Here the simple name x is used to mean this.x in one local scope and the local variable x in a nested scope. This is confusing; a simple name is required to mean the same thing throughout its entire block. We therefore make it illegal. This would be legal:

    class C
    {
        int x;
        void M()
        {
            if (whatever)
            {
                string x = "";
            }
        }
    }
    

    Even though the local variable is declared in a scope nested inside the scope of the field, this is allowed because the field’s scope is not a local variable declaration space.

    This is also legal:

    class C
    {
        int x;
        void M()
        {
            if (whatever)
            {
                string x = "";
            }
            else
            {
                x = 2;
            }
        }
    }
    

    because now the two conflicting usages of x are both used consistently throughout the entirity of their immediately containing scopes. Those scopes now do not overlap, so this is allowed. (It is still a bad idea however.)

    These rules are there for your safety but they can be quite confusing. See

    http://blogs.msdn.com/b/ericlippert/archive/tags/declaration+spaces/

    and

    http://blogs.msdn.com/b/ericlippert/archive/tags/scope/

    for some articles on these language features.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.