Static void main ()
{
Int I;
For (I = 0; I <10; I ++)
{
String text = "line" + covert. tostring (I );
Console. writeline ("{0}", text );
}
Console. writeline ("last text output in loop: {0}", text );
Console. readkey ();
}
The character string constant text is a local variable in the for loop. This Code cannot be compiled. The variable is called outside the loop and beyond the loop scope. The modified code is as follows:
Static void main ()
{
Int I;
String text;
For (I = 0; I <10; I ++)
{
String text = "line" + covert. tostring (I );
Console. writeline ("{0}", text );
}
Console. writeline ("last text output in loop: {0}", text );
Console. readkey ();
}
This Code also fails because the declared variable is not initialized and the initialization in the loop fails after the loop ends. The modified code is as follows:
Static void main ()
{
Int I;
String text = "";
For (I = 0; I <10; I ++)
{
String text = "line" + covert. tostring (I );
Console. writeline ("{0}", text );
}
Console. writeline ("last text output in loop: {0}", text );
Console. readkey ();
}