我有一个简单的课程
class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime HireDate { get; set; }
public Employee()
{
}
public static Employee GetNewestEmployee(List<Employee> e)
{
Employee emp = e.OrderByDescending(x => x.HireDate).FirstOrDefault();
Console.WriteLine("The newest employee was hired on {0}",emp.HireDate);
return emp;
}
该方法将返回最近雇用的员工。在我的主要方法中,我有类似的东西
Employee e = new Employee();
e.EmployeeId = 101;
e.FirstName = "Tom";
e.LastName = "Jones";
e.HireDate = DateTime.Now.Subtract(TimeSpan.FromDays(40));
Employee e2 = new Employee();
e2.EmployeeId = 102;
e2.HireDate = DateTime.Now.Subtract(TimeSpan.FromDays(100));
Employee e3 = new Employee();
e3.EmployeeId = 103;
e3.HireDate = DateTime.Now.Subtract(TimeSpan.FromDays(1000));
List<Employee> empList = new List<Employee>();
empList.Add(e);
empList.Add(e2);
empList.Add(e3);
Console.WriteLine(Employee.GetNewestEmployee(empList).EmployeeId);
}
所有这一切都很好,但我想知道如果您不知道要创建多少个 Employee 对象,或者创建了 1000 个员工对象,那么解决方案是什么。一个人怎么能做这样的事情foreach(object o thatHasBeenInstantiated)
?