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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:22:29+00:00 2026-05-31T05:22:29+00:00

private void Abc() { string first=; ArrayList result = new ArrayList(); ArrayList secResult =

  • 0
private void Abc()
{
    string first="";
    ArrayList result = new ArrayList();
    ArrayList secResult = new ArrayList();
    foreach (string second in result)
    {
        if (first != null)
        {
            foreach (string third in secResult)
            {
                string target;
            }
        }

        string target;//Here I cannot decalre it. And if I don't declare it and
        //want to use it then also I cannot use it. And if it is in if condition like
        //the code commented below, then there is no any complier error.
        //if (first != null)
        //{
        //    string target;
        //}
    }
}

I cannot understand: Why can’t I declare the variable outside the foreach loop, as compiler gives an error that the variable is already declared. The scope of the foreach (and hence the target variable) is over where I am declaring this new variable.

  • 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-31T05:22:29+00:00Added an answer on May 31, 2026 at 5:22 am

    The scope of a local variable extends all the way up to the start of the block in which it’s declared. So the scope of your second declaration as actually the whole of the outer foreach loop. From the C# 4 spec, section 3.7:

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

    and in section 8.5.1:

    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.

    So even though the second variable hasn’t been declared at the point where the first variable occurs, it’s still in scope – so the two declarations between them violate 8.5.1.

    The language was designed this way to prevent errors – it would be odd if simply moving the location of a local variable declaration within the block in which it’s declared and before its first use changed the validity of the code.

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

Sidebar

Related Questions

private void BindCountry() { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath(countries.xml)); foreach (XmlNode node in
. private void Form1_Load(object sender, EventArgs e) { List<CaclulatedData> tests = new List<CaclulatedData> {
private void StringAction(string aString) // method to be called { return; } private void
private void EnsureCurrentlyValid() { //I'm valid if IDataErrorInfo.this[] returns null for every property var
private void validateXML(DOMSource source) throws Exception { URL schemaFile = new URL(http://www.csc.liv.ac.uk/~valli/modules.xsd); SchemaFactory schemaFactory
private void save(Rectangle src, Rectangle dest, string fName, int width, int height, Bitmap file)
I have this code private void writeReport(IReport report, string reportName) { string reportString =
public class abc1 { private String s; public abc1(String s){this.s=s;} public static void main(String
class Program { static void Main(string[] args) { List<A> myList = new List<A> {new
List item I use following Regex to validate a string ^[a-zA-Z0-9-/]* private static void

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.