There is a code:

using System; namespace Acces_control { public class Worker : Human { public int work_experience { get { return work_experience; } set { if ((value <= 90) && (value >= 0)) { work_experience = value; } else { throw new ArgumentOutOfRangeException(); } } } public string ZoneOfWork { get { return ZoneOfWork; } set { ZoneOfWork = value; } } public Worker() { work_experience = 0; ZoneOfWork = "NA"; } } } 

PS: the Human class contains just a couple of variables of type Name and LastName When I change the work_experience variable:

 var worker = new Worker(); worker.work_experience = 10; 

I get the following error:

 Stack overflow: IP: 0x4018a914, fault addr: 0x7ffefce96ff0 Stacktrace: at Acces_control.Worker.set_work_experience (int) [0x00013] in /home/vasya/Programming/C#/Tests/Classes/Acces_control/Worker.cs:11 <...> at Acces_control.Worker..ctor () [0x00009] in /home/vasya/Programming/C#/Tests/Classes/Acces_control/Worker.cs:25 at Acces_control.MainClass.Main (string[]) [0x00001] in /home/vasya/Programming/C#/Tests/Classes/Acces_control/Program.cs:9 at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00058, 0xffffffff> Press any key to continue... 
  • When the Worker class is initialized, the work_experience = 0; property is initialized. because condition (value <= 90) && (value> = 0) fails throw exception throw throw ArgumentOutOfRangeException (); - NMD
  • @NMD, (0 <= 90) && (0> = 0) is not executed? .. - Alekcvp
  • oh exactly @Alekcvp you are right. then it is not clear what - NMD
  • 3
    Alekcvp is right: a recursive access to a property occurs. You must enter a storage field and set it in the properties setter. - Alexander Petrov

1 answer 1

Stack overflow most often means endless recursion. You in get / set of this property refer to it, trying to get / change its value, and, accordingly, you get infinite recursion. See how it’s done here (see below) - it uses a separate variable to store the property value:

 public class Date { private int month = 7; // тут хранится значение свойства public int Month { get { return month; } set { if ((value > 0) && (value < 13)) { month = value; } } } } 
  • Did not help. All the same - Stack Overflow. - cheloca
  • It may not be necessary to add the variable private int _work_experience = 0; and in the getter to take the value from it, and in the setter to enter the value into it. yet painted @Alekcvp - NMD
  • I don `t know @NMD, but it is possible. - cheloca
  • one
    @cheloca show the code so that you can be helped - NMD