开发者

How do I access a variable outside of an foreach (C#)

开发者 https://www.devze.com 2023-03-18 04:12 出处:网络
I\'m trying to get the variable result from a foreach in my main method. The code looks like this: static void Main(string[] args)

I'm trying to get the variable result from a foreach in my main method. The code looks like this:

    static void Main(string[] args)
    {

   开发者_如何学Go     ArrayList lines = GetLines("test.txt", "8394", true);
        foreach (string s in lines)
        {
            string result = s;
        }
        Console.WriteLine(result);
    }

As you can see it returns an error because I cannot access the variable outside of the foreach. How do I access it?


I cannot understand what do you want to achieve... but the code should be this:

 ArrayList lines = GetLines("test.txt", "8394", true);
 string result=string.Empty;       
 foreach (string s in lines)
        {
            result = s;
        }
        Console.WriteLine(result);

I think you want do to something like this:

 ArrayList lines = GetLines("test.txt", "8394", true);

 foreach (string s in lines)
        {
           Console.WriteLine(s);
        }


Your code is wrong logically. You loop through some lines, assign them to a variable, then do nothing to it, then assign the next line to a new variable (each time the loop gets another line, another variable called result is created), and so on.

This could be considered a logical code:

string names = string.Empty;
foreach (string name in namesList)
{
    names += ", " + name;
}
console.WriteLine(names);


Declare string result outside the foreach loop

string result = "" ;

foreach (string name in namesList)
{
    names += ", " + name;
}

....etc


you need to declare a variable outside the braces {} to use it outside

ArrayList lines = GetLines("test.txt", "8394", true);
 string result;       
 foreach (string s in lines)
        {
            result = s;
        }
        Console.WriteLine(result);

Is the obvious answer, however this code will only write out to the console the last parameter/item in the lines array.

It all depends what you want really, if you want to write out all the contents of the lines array, you can write line for each item by doing it within the loop. e.g.

ArrayList lines = GetLines("test.txt", "8394", true);
 string result;       
 foreach (string s in lines)
        {
            result = s;
            Console.WriteLine(result);
        }

Would write every item


The scope of any variable declared within the foreach loop is limited to the loop itself. It cannot be accessed outside the loop. As @danyolgiax mentioned, declare the variable outside the loop. Then you will be able to access it.


Variables can be accessed with in the scope. So if you want to access a variable outside the foreach loop, then declare it outside the loop.

string result = string.Empty;

foreach (string s in lines)         
{              
   result = s;         
} 


in case u want to get just current value of as above code is ok but if you want to get line you shoul write

    static void Main(string[] args)
    {
        string result = string.Empty;
        ArrayList lines = GetLines("test.txt", "8394", true);
        foreach (string s in lines)
        {
            result += s;
        }
        Console.WriteLine(result);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消