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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:07:56+00:00 2026-05-24T07:07:56+00:00

Possible Duplicates: confused with the scope in c# C# Variable Scoping I am curious

  • 0

Possible Duplicates:
confused with the scope in c#
C# Variable Scoping

I am curious about the design considerations behind the scope of variables which are declared in the initialization part of for-loops (etc). Such variables neither seem to be in-scope or out-of scope or am I missing something? Why is this and when is this desired? Ie:

for (int i = 0; i < 10; i++)
{
}

i = 12;       //CS0103: The name 'i' does not exist in the current context

int i = 13;   //CS0136: 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
  • 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-24T07:07:56+00:00Added an answer on May 24, 2026 at 7:07 am

    The loop variable is scoped to the loop itself. This is why you see the expected result of i not being available outside the loop.

    The fact you can’t declare i outside the loop is a bit more puzzling but is to do with the fact that once compiled all the variable declarations can be considered to be at the beginning of the block they are declared in. That is your code in practice is the same as:

    int i;
    
    for (int i = 0; i < 10; i++)
    {
    }
    
    i = 13; 
    

    Hopefully it is obvious here that you have a name collision. As for why it works like that I can’t tell you for sure. I’m not that up on what compilers do under the hood but hopefully somebody else will pop up and explain why.

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

Sidebar

Related Questions

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about
Possible Duplicates: Htmlentities vs addslashes vs mysqli_real_escape_string When to use which string escaping method?
Possible Duplicate: Property Declaration and Automatic Backing Storage Allocation I'm really confused about this
Possible Duplicate: Properties and Instance Variables in Objective-C 2.0 I'm confused by these two
Possible Duplicate: What do the brackets mean in x86 asm? I've been confused about
Possible Duplicate: Static vs global I'm confused about the differences between global and static
Possible Duplicate: Why is my return type meaningless? Hi, I'm confused about a particular
Possible Duplicates: C++0X when? When will C++0x be finished? When will C++0x be released?
Possible Duplicates: Why is lock(this) {...} bad? In C# it is common to use
Possible Duplicates: What is the best approach for a Java developer to learn C++

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.