[Mono-devel-list] Basic C# help

Phil Frost indigo at bitglue.com
Fri Dec 10 16:44:28 EST 2004

On Fri, Dec 10, 2004 at 03:21:03PM -0600, Eric Scott wrote:
> Hey;
> I'm newbie to C#, and have been getting an error in some simple code I'm 
> doing.  The error is right after the following while loop executes:
>  int totalrevenue = 0;
>       user = 0;
>       while(user <= revenue.GetLength(0)) {
>       user ++;
>       totalrevenue += revenue[user];
>       }
> Compiles fine, but when I run the exe file I get:
> Unhandled Exception: System.IndexOutOfRangeException: Array index is out 
> of range.
> in <0x00255> Test:Main (string[])

I don't know C# at all, but if I had to guess I'd say that the problem
is where you do "revenue[user]". I bet array indexes start at 0 and thus
the largest valid index is one less than the size of the array. In your
while loop you use "<=" where it should be "<", causing iteration to
continue exactly one cycle too long.

More information about the Mono-devel-list mailing list