В этом коде метод CompareTo () не вызывается явно, а max () дает правильный результат. Итак, мой вопрос: возможно ли, что CompareTo () вызывается неявно / auto-call? Если так, как я узнаю, какая другая функция / метод может быть вызвана неявно? Пожалуйста, помогите мне понять, спасибо! Результаты: Стив
public class Student : IComparable<Student>
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
public int StandardID { get; set; }
public int CompareTo(Student other)
{
if (this.StudentName.Length >= other.StudentName.Length)
return 1;
return 0;
}
}
class Program
{
static void Main(string[] args)
{
// Student collection
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Steve" , Age = 15 }
};
var studentWithLongName = studentList.Max();
Console.WriteLine("Student Name: {1}", studentWithLongName.StudentName);
}
}