R
raylopez99
Why is the same variable local inside a 'foreach' loop yet 'global' in
scope (or to the class) outside it?
RL
class MyClass
{
int[] MyMemberArray1; //member variables, arrays, that are "global" to
the class
char[] MyMemberArray2;
string[] AnotherArray;
public void MyFunction()
{
MyMemberArray1 = new int[10]; //'global' here; compare
with below
MyMemberArray2 = new char[10];
foreach (string s in AnotherArray)
{
// MyMemberArray1 = new int[10]; //if placed here, only
local to 'foreach' despite being a member of class MyClass!
// MyMemberArray2 = new char[10]; //Ditto
}
}
}
scope (or to the class) outside it?
RL
class MyClass
{
int[] MyMemberArray1; //member variables, arrays, that are "global" to
the class
char[] MyMemberArray2;
string[] AnotherArray;
public void MyFunction()
{
MyMemberArray1 = new int[10]; //'global' here; compare
with below
MyMemberArray2 = new char[10];
foreach (string s in AnotherArray)
{
// MyMemberArray1 = new int[10]; //if placed here, only
local to 'foreach' despite being a member of class MyClass!
// MyMemberArray2 = new char[10]; //Ditto
}
}
}