0

I am learning C# and was trying different ways to add to list. 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 :)

6
  • Both look good to me... Did you use the debugger? Any messages? Commented Feb 10, 2015 at 6:13
  • What is the exception if any ? Commented Feb 10, 2015 at 6:14
  • use debug next time .. Commented Feb 10, 2015 at 6:16
  • @ziddarth It shows StackOverflowException Commented Feb 10, 2015 at 6:17
  • @mybirthname Just started learning C# and new to visual studio too :) Will take your suggestion :) Commented Feb 10, 2015 at 6:18

3 Answers 3

1

It's easy, in the first case you construct your Employee which constructs more Employees and so on and so forth.

In fact if you bothered to paste the exception you got, it would be readily obvious: StackOverflowException.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes that is what the exception i am getting. What is get around for it? Thanks :)
Delete all you wrote, it makes no sense for an employee to hold a list of employees. Think about what you mean to model, write it on a whiteboard if you need to, and start over.
So the second method is fine?? I can do it that way right?
No. Keep class employee and "List of emplyees" separate! An employee should not contain a list of all employees, that is bad design. In a later stage you may have a "chief" with people which report to him, but that's a different story...
@DrKoch so should i create employee list in different class?? Like in main method??
0

Your first code cause infinite looping as you are adding same class Employee in Constructor.

Comments

0

Actually, program goes to infinite loop in constructor and never complete.

In Main():

from this line : 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 });

At this line, you are adding new Employee object to list. Here object initialization again, and thus program goes to infinite loop in constructor.

Your program will never reach at final line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.