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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:24:14+00:00 2026-05-26T16:24:14+00:00

I have been using C# for quite a long time but never realised the

  • 0

I have been using C# for quite a long time but never realised the following:

 public static void Main()
 {
     for (int i = 0; i < 5; i++)
     {

     }

     int i = 4;  //cannot declare as 'i' is declared in child scope                
     int A = i;  //cannot assign as 'i' does not exist in this context
 }

So why can I not use the value of ‘i’ outside of the for block if it does not allow me to declare a variable with this name?

I thought that the iterator variable used by a for-loop is valid only in its 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-26T16:24:15+00:00Added an answer on May 26, 2026 at 4:24 pm

    The reason you are not allowed to define a variable with the same name in both the for-loop as well as outside the for-loop is because variables in the outer-scope are valid in the inner-scope. Meaning that there would be two ‘i’ variables within the for-loop if this was allowed.

    See: MSDN Scopes

    Specifically:

    The scope of a local variable declared in a local-variable-declaration
    (Section 8.5.1) is the block in which the declaration occurs.

    and

    The scope of a local variable declared in a for-initializer of a for
    statement (Section 8.8.3) is the for-initializer, the for-condition,
    the for-iterator, and the contained statement of the for statement.

    And also: Local variable declarations (Section 8.5.1 of the C# specification)

    Specifically:

    The scope of a local variable declared in a local-variable-declaration
    is the block in which the declaration occurs.
    It is an error to refer
    to a local variable in a textual position that precedes the
    local-variable-declarator of the local variable. Within the scope of a
    local variable, it is a compile-time error to declare another local
    variable or constant with the same name.

    (Emphasis mine.)

    Which means that the scope of the i inside your for-loop, is the for-loop. Whereas the scope of the i outside of your for-loop is the entire main method plus the for-loop. Meaning you’d have two occurrences of i inside the loop which is invalid according to the above.

    The reason why you’re not allowed to do int A = i; is because int i is only scoped for use within the for loop. Thus it is no longer accessible outside of the for loop.

    As you can see both of these issues are a result of scoping; the first issue (int i = 4;) would result in two i variables within the for loop scope. Whereas int A = i; would result in access to a variable that is out of scope.

    What you could do instead is declare i to be scoped to the entire method, and then use it in both the method as well as the for-loop scope. This will avoid breaking either rule.

    public static void Main()
    {
        int i;
    
        for (i = 0; i < 5; i++)
        {
    
        }
    
        // 'i' is only declared in the method scope now, 
        // no longer in the child scope -> valid.
        i = 4;
    
        // 'i' is declared in the method's scope -> valid. 
        int A = i;
    }
    

    EDIT:

    The C# compiler could of course be changed to allow this code to compile quite validly. After all this is valid:

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine(i);
    }
    
    for (int i = 5; i > 0; i--)
    {
        Console.WriteLine(i);
    }
    

    But would it really be beneficial to your code readability and maintainability to be able to write code such as:

    public static void Main()
    {
        int i = 4;
    
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(i);
        }
    
        for (int i = 5; i > 0; i--)
        {
            Console.WriteLine(i);
        }
    
        Console.WriteLine(i);
    }
    

    Think about the potential for mistakes here, does the last i print out 0 or 4? Now this is a very small example, one which is quite easy to follow and track but it is definitely a lot less maintainable and readable than having declared the outer i by a different name.

    N.B:

    Please note, C#’s scoping rules differ from C++’s scoping rules. In C++ variables are only in scope from where they are declared until the end of the block. Which would make your code a valid construct in C++.

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

Sidebar

Related Questions

I have been using Visual Studio 2008 quite long but lately I am getting
I have been using Java for a long time, and for quite some time
I have been using ReSharper for quite a long time and get used to
I've been using multiple inheritance in c++ for quite a long time, but only
I have been using ASP.NET for years, but I can never remember when using
I have been using Visual Studio (VC++) and Windows API for a long time,
I have been using the jquery appear plugin for some time now, but it
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using this copy method for quite a while, in lots of
I've been using extension methods quite a bit recently and have found a lot

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.