コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("====Non Generic====");
            ListIterator employeeList = new ListIterator();
            employeeList.Add("Jane", Employee.TypeEnum.Manager);
            employeeList.Add("Tom", Employee.TypeEnum.Manager);
            employeeList.Add("Brian", Employee.TypeEnum.Associate);
            employeeList.Add("Ashley", Employee.TypeEnum.Associate);
            employeeList.Add("Meriam", Employee.TypeEnum.Associate);

            foreach (var associate in employeeList.Associates)
            {
                Console.WriteLine("Associate {0}", ((Employee)associate).Name);//casting
            }

            foreach (var manager in employeeList.Managers)
            {
                Console.WriteLine("Manager {0}", ((Employee)manager).Name);//casting
            }

            foreach (var employee in employeeList)
            {
                Console.WriteLine("Employee {0}", ((Employee)employee).Name);//casting
            }
            Console.WriteLine();
            Console.WriteLine("====Generic====");
            ListIterator<Employee> employeeGenList = new ListIterator<Employee>();
            employeeGenList.Add(new Employee { Name = "Jane", Type = Employee.TypeEnum.Manager });
            employeeGenList.Add(new Employee { Name = "Tom", Type = Employee.TypeEnum.Manager });
            employeeGenList.Add(new Employee { Name = "Brian", Type = Employee.TypeEnum.Associate });
            employeeGenList.Add(new Employee { Name = "Ashley", Type = Employee.TypeEnum.Associate });
            employeeGenList.Add(new Employee { Name = "Meriam", Type = Employee.TypeEnum.Associate });
            foreach (var employee in employeeGenList.Where(e => e.Type == Employee.TypeEnum.Associate))
            {
                Console.WriteLine("Associate {0}", employee.Name);//no casting
            }
            foreach (var employee in employeeGenList.Where(e => e.Type == Employee.TypeEnum.Manager))
            {
                Console.WriteLine("Manager {0}", employee.Name);//no casting
            }
            foreach (var employee in employeeGenList)
            {
                Console.WriteLine("Employee {0}", employee.Name);//no casting
            }
            Console.ReadKey();
        }