I am reading Beginning C# to refresh my memory on C# (background in C++).
I came across this snippet in the book:
int i;
string text;
for (i = 0; i < 10; i++)
{
text = "Line " + Convert.ToString(i);
Console.WriteLine("{0}", text);
}
Console.WriteLine("Last text output in loop: {0}", text);
The snippet above will not compile - because according to the book, the variable text is not initialized, (only initialized in the loop - and the value last assigned to it is lost when the loop block is exited.
I can't understand why the value assigned to an L value is lost just because the scope in which the R value was created has been exited - even though the L value is still in scope.
Can anyone explain why the variable text loses the value assigned in the loop?.