I'm getting the following error:
Use of unassigned local variable.
Code:
int c;
for (int b = 1; b < 5; b++)
{
c = b * 2;
}
I'm getting the following error:
Use of unassigned local variable.
Code:
int c;
for (int b = 1; b < 5; b++)
{
c = b * 2;
}
replace
int c;
with
int c = 0; //or some other initial value
The error appears because the compiler doesn't know if the loop is ever excecuted / a value is assigned to c. So it doesn't allow you to use it Console.WriteLine(c);
int c; // this works because the compiler knows there is a values assigned to c
c = 1;
Console.WriteLine(c);