You can define a string variable in the body of a
for
loop without problems:
for (int i = 0; i < 10, i++)
{
string myString = i.ToString();
...
}
But you can only use the string within that body - you cannot use it outside the loop as it is "out of scope" and can no longer be seen. Variables can only be used inside the "curly bracket block" within which they are declared:
{
...
int i1 = 1;
...
{
...
int i2 = 2;
...
}
...
}
You can also define a string as the loop control variable of a
for
loop:
for (string s = "0"; s.Length < 10; s += "0")
{
Console.WriteLine(s);
}
But obviously, you can't use ++s or s++ in the "increment" section. The scope of "s" remains the same - if it not visible to code outside the loop.
For any more help, we'd need a lot more info on exactly what code you have written!