繁体   English   中英

使用构造函数C#将项目添加到列表

[英]Adding item to list using constructor C#

我正在学习C#,并尝试使用其他方法添加到列表中。 我在下面尝试了两种不同的方法。 第一个无效 ,第二个有效

第一种方法有什么问题?

 class Program
    {
        static void Main(string[] args)
        {
            Employee emps = new Employee();
            emps.PromoteEmp(emps.emp);
        }
    }

    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }
        public List<Employee> emp;

        public Employee()
        {
            emp = new List<Employee>();
            emp.Add(new Employee() { ID = 1, Name = "A", Experience = 6, Salary = 30000 });
            emp.Add(new Employee() { ID = 2, Name = "B", Experience = 4, Salary = 10000 });
            emp.Add(new Employee() { ID = 1, Name = "C", Experience = 5, Salary = 15000 });
            emp.Add(new Employee() { ID = 1, Name = "D", Experience = 8, Salary = 60000 });
        }

        public void PromoteEmp(List<Employee> empList)
        {
            foreach (Employee item in empList)
            {
                if (item.Experience > 5)
                {
                    Console.WriteLine(item.Name + " promoted ");
                }
            }
        }
    }

第二种方法

 class Program
    {
        static void Main(string[] args)
        {
            Employee emps = new Employee();
            emps.AddToList();
            emps.PromoteEmp(emps.emp);
        }
    }

    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }
        public List<Employee> emp;


        public void AddToList()
        {
          emp = new List<Employee>();
          emp.Add(new Employee() { ID = 1, Name = "A", Experience = 6, Salary = 30000 });
          emp.Add(new Employee() { ID = 2, Name = "B", Experience = 4, Salary = 10000 });
         emp.Add(new Employee() { ID = 1, Name = "C", Experience = 5, Salary = 15000 });
         emp.Add(new Employee() { ID = 1, Name = "D", Experience = 8, Salary = 60000 });
        }

        public void PromoteEmp(List<Employee> empList)
        {
            foreach (Employee item in empList)
            {
                if (item.Experience > 5)
                {
                    Console.WriteLine(item.Name + " promoted ");
                }
            }
        }
    }

谢谢 :)

很简单,在第一种情况下,您构造了Employee ,而后者构造了更多Employee ,依此类推。

实际上,如果您不愿意粘贴所获得的异常,则显而易见: StackOverflowException

您的第一个代码会导致无限循环,因为要在Constructor中添加相同的Employee类。

实际上,程序在构造函数中进入无限循环,并且永远不会完成。

在Main()中:

从这一行开始:Employee emps = new Employee();

程序转到构造函数初始化对象。

现在在构造函数中:

emp.Add(new Employee(){ID = 1,名称=“ A”,经验= 6,工资= 30000});

在这一行,您要向列表添加新的Employee对象。 这里再次进行对象初始化,因此程序进入构造函数中的无限循环。

您的程序将永远无法到达最后一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM