static void Main(string[] args) { // Create a new empty List for Employee objects List <Employee> empList = new List <Employee>(); // Add some records to the list empList.Add(new Employee("John Doe", 50000)); empList.Add(new Employee("Jane Smith", 60000)); empList.Add(new Employee("Nick Slick", 55000)); empList.Add(new Employee("Mildred Mintz", 70000)); // Inspect some List properties Console.WriteLine("empList Capacity is: {0}", empList.Capacity); Console.WriteLine("empList Count is: {0}", empList.Count); // Use Exists() and Find() if (empList.Exists(HighPay)) { Console.WriteLine("\nHighly Paid Employee Found!\n"); } Employee e = empList.Find(x => x.mName.StartsWith("J")); if (e != null) { Console.WriteLine("Found employee whose name starts with J: {0}", e.mName); } // Use ForEach to iterate over each item empList.ForEach(TotalSalaries); Console.WriteLine("Total payroll is: {0}\n", total); // Sort the list using a custom class // that implements the IComparer interface EmployeeComparer ec = new EmployeeComparer(); empList.Sort(ec); foreach (Employee emp in empList) { Console.WriteLine("Salary for {0} is {1}", emp.mName, emp.mSalary); } Console.WriteLine("\nPress Enter key to continue..."); Console.ReadLine(); }
static void Main(string[] args) { List <Employee> empList = new List <Employee>(10); empList.Add(new Employee("Dillorom", 5000)); empList.Add(new Employee("Iymona", 6000)); empList.Add(new Employee("Amina", 7000)); Console.WriteLine("The list capacity is " + empList.Capacity); Console.WriteLine("Employee size is " + empList.Count); if (empList.Exists(HighPay)) { Console.WriteLine("\nHighly Paid Employee Found!\n"); } empList.ForEach(TotalSalaries); Console.WriteLine("Total payroll is: {0}\n", total); Employee e = empList.Find(x => x.mName.StartsWith("A")); if (e != null) { Console.WriteLine($"Found employee whose name starts with A:{e.mName}"); } EmployeeComparer ec = new EmployeeComparer(); empList.Sort(ec); foreach (Employee emp in empList) { Console.WriteLine("Salary for {0} is {1}", emp.mName, emp.mSalary); } Console.WriteLine("\nPress Enter key to continue..."); Console.ReadLine(); }