using System;
using System.Collections.Generic;
namespace ConsoleApp3
{
public class Student
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
public Student(int id, string name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
public void ShowInfo(string info)
{
Console.WriteLine("{0}:\n{1}", info, this.ToString());
}
}
class Program
{
public delegate void TestDelegate(string info);
static void Main(string[] args)
{
//创建一个泛型列表, 列表中的元素类型为Student
List<Student> stuList = new List<Student>();
stuList.Add(new Student(3, "张三", 20));
stuList.Add(new Student(1, "李四", 15));
stuList.Add(new Student(2, "王五", 18));
//遍历并输出
foreach (var stu in stuList)
{
Console.WriteLine(stu.name);
}
Console.WriteLine("排序后:");
//使用匿名委托完成排序
stuList.Sort(delegate (Student s1, Student s2)
{
return s2.age - s1.age;
});
//遍历并输出
foreach (var stu in stuList)
{
Console.WriteLine(stu.name);
}
}
}
}
这段代码说的使用匿名委托进行排序是怎么排序的?工作原理是什么?谢谢指点!