int i;
string text;
for (i = 0; i <; i++)
{
Text = "line" +convert.tostring (i);
Console.WriteLine ("{0}", text);
}
Console.WriteLine ("Last text in loop: {0}", text);
This code compilation also fails, the variable text must be declared and initialized before use, and text is initialized in the loop, and the value assigned to text is lost after the loop exits.
int i;
String text= "";
for (i = 0; i <; i++)
{
Text = "line" +convert.tostring (i);
Console.WriteLine ("{0}", text);
}
Console.WriteLine ("Last text in loop: {0}", text);
Only one simple type is declared, and there is no change, and no memory is allocated to the variable. A variable occupies only one piece of memory after it is assigned. If this memory-hogging behavior occurs only in loops, the value is actually a local variable that is outside the scope of the loop. Assigning a value outside the loop ensures that the value is a local value of the body code and is still within the scope of the value inside the loop.
Declaration and initialization of variables in C #