I am new to C# and I have wrote a program that gives me index out of bound in specific program. Theoretically I see no reason why should i get this. The error is outputted in console but doesn't crash the program. I have 3 classes Logic, Student and Main. When I try to to print out a list of all the students I get an error. Student contains a Name, FN, and array of grades in form of double.
in Student.cs The students are added into a list(not shown here) also the grades are with capacity of 40 but student doesn't practically have 40 grades so the empty spaces are nulls->
public Student (string namee, int FNe, double[] gradese)
{
name = namee;
FN = FNe;
grades = gradese;
}
public void print()
{
Console.WriteLine(name);
Console.WriteLine(FN);
double z = 0;
int j = 0;
while(grades[j] != 0)
{
Console.Write(grades[j]);
z += grades[j];
j++;
}
Console.WriteLine($"average: {z/j}" );
}
In "logic.cs" The students are added into a list(not shown here) also the grades are with capacity of 40 but student doesn't practically have 40 grades so the empty spaces are nulls->
List<Student> myList = new List<Student>();
public void print()
{
for (int i = 0; i < myList.Count ; i++)
{
myList[i].print();
}
}
The error i get is this but in the console:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at ConsoleApp6.Student.print() in C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\Class1.cs:line 73 at ConsoleApp6.Logic.print() in C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\Class2.cs:line 37 at ConsoleApp6.Program.Main(String[] args) in C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\Program.cs:line 22 Press any key to continue . . .
Line 73 and 37 are the loops (while and for) respectively in Student and Logic