简体   繁体   English

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

[英]Adding item to list using constructor C#

I am learning C# and was trying different ways to add to list. 我正在学习C#,并尝试使用其他方法添加到列表中。 I tried two different method below . 我在下面尝试了两种不同的方法。 First One does not work , second one does work . 第一个无效 ,第二个有效

What is wrong with first method? 第一种方法有什么问题?

 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 ");
                }
            }
        }
    }

Second Method 第二种方法

 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 ");
                }
            }
        }
    }

Thank You :) 谢谢 :)

It's easy, in the first case you construct your Employee which constructs more Employee s and so on and so forth. 很简单,在第一种情况下,您构造了Employee ,而后者构造了更多Employee ,依此类推。

In fact if you bothered to paste the exception you got, it would be readily obvious: StackOverflowException . 实际上,如果您不愿意粘贴所获得的异常,则显而易见: StackOverflowException

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

Actually, program goes to infinite loop in constructor and never complete. 实际上,程序在构造函数中进入无限循环,并且永远不会完成。

In Main(): 在Main()中:

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

program goes to constructor to initialize object. 程序转到构造函数初始化对象。

Now in constructor: 现在在构造函数中:

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

At this line, you are adding new Employee object to list. 在这一行,您要向列表添加新的Employee对象。 Here object initialization again, and thus program goes to infinite loop in constructor. 这里再次进行对象初始化,因此程序进入构造函数中的无限循环。

Your program will never reach at final line. 您的程序将永远无法到达最后一行。

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

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