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.