Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
interface IEmployee
   {
       string this[int Id]
       {
           get;
           set;
       }



   }


   class Employee
   {
       int EmpId { get; set; }
       string Name { get; set; }
       string Gender { get; set; }
       private  List<Employee> listemp;
       public Employee()
       {
           listemp = new List<Employee>()
           {

               new Employee(){EmpId=101,Name="Sarita",Gender="Male"},
               new Employee(){EmpId=102,Name="Lalita",Gender="Female"}
           };



       }





       public string this[int Id]
       {
           get { return listemp.FirstOrDefault(x => x.EmpId == Id).Name; }
           set { listemp.FirstOrDefault(x => x.EmpId == Id).Name = value; }
       }


   }


   class Program
   {
       static void Main(string[] args)
       {

           Employee emp=new Employee();
           Console.WriteLine("Name is {0}", emp[101]);
           Console.WriteLine("Name is {0}", emp[102]);
           Console.ReadKey();

       }
   }


What I have tried:

whenever I run it's give exception stack overflow, then how do I resolve this problem
Posted
Updated 12-Feb-20 4:06am

Quote:
C#
public Employee()
{
    listemp = new List<Employee>()
    {
        new Employee(){EmpId=101,Name="Sarita",Gender="Male"},
        new Employee(){EmpId=102,Name="Lalita",Gender="Female"}
    };
}
It should be pretty obvious what the problem is: you create a new instance of the Employee class, which tries to create two instances of the Employee class, each of which tries to create two instances of the Employee class, each of which...

You have infinite recursion in your code. You'll need to remove that.
 
Share this answer
 
Look at what happens when you call the constructor:
- listemp is initialized with a new list of employees for the initial employee
- a first item is given to the list, calling the constructor again
- listemp is initialized with a new list of employees for the first item of listemp
- etc...

You run into this issue because your are calling the Employee constructor from within itself. The program keeps creating new lists of employees and try to affect them new employees.

What you probably want instead is just create a new list without populating it (so that is is not null). Later, when initialized, you can populate emplist with relevant employees.

Put a breakpoint in your constructor, press F5, and debug from there. You will clearly see what is going on.
 
Share this answer
 
v3
The Employee constructor calls itself!
 
Share this answer
 
Quote:
whenever I run it's give exception stack overflow, then how do I resolve this problem

Use the debugger to see what is going on your code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900