everyone! I'm new in programming so I hope for your help! I'm trying to return and index of the array but have no idea what does the error mean. I got this one for the first time.
this is my first class
using System;
using System.Linq;
public class Group
{
private Student[] students;
public Group(Student[] students)
{
if(students.Length < 5)
{
throw new ArgumentException("Incorrect group");
}
this.students = students;
}
public int IndexOfMaxGrade()
{
Student[] sorted = students.OrderBy(c => c.grade).ToArray();
//int max = sorted[^1].grade;
//foreach (var item in students)
//{
// if(item.grade == max)
// {
// }
//}
return Array.IndexOf(students, sorted[^1].grade);
}
public int IndexOfMinGrade()
{
Student[] sorted = students.OrderBy(c => c.grade).ToArray();
return Array.IndexOf(students, sorted[0].grade);
}
}
and I got on "Cannot apply indexing with [] to an expression of type 'type'" on this code (last 2 lines)
Group studentGroup;
try
{
studentGroup = new Group(students.ToArray());
}
catch (ArgumentException argumentException)
{
Console.WriteLine(argumentException.Message);
return;
}
Console.WriteLine(studentGroup[studentGroup.IndexOfMinGrade()]);
Console.WriteLine(studentGroup[studentGroup.IndexOfMaxGrade()]);
}
}