[RESOLVED] [1.0/1.1] Loop problem?
I only started C# today (i already know VBNET), and can't work out whats wrong with this loop:
Code:
static void Main(string[] args)
{
int[] i = new int[3];
i[0] = 2;
i[1] = 4;
i[2] = 61;
int sum;
for (int x = 0; x < 2; x++)
{
sum += i[x];
}
Console.WriteLine(sum);
Console.ReadLine();
}
Sorry for such a simple question.
Re: [1.0/1.1] Loop problem?
Wait....i know. x doesn't have a value yet, so it can't be used.
Re: [RESOLVED] [1.0/1.1] Loop problem?
It's not 'x' that's the issue. It's 'sum'. You need to initialise all variables in C#.This is one of the ways in which C# is stricter than VB.NET. If you want 'sum' to be zero then you have to specify it. That way you actually have to think about what value you want a variable to have and you won't simply forget to set a value and have a variable default to zero when that isn't what you want.
Re: [RESOLVED] [1.0/1.1] Loop problem?