When a field is accessed to get a value for which get and set are specified, multiple accesses are made to this field, which results in a StackOverflowException . If we make the field available, i.e. public , the problem disappears.

 class Sphere : Figure { public int radius { get { return radius; } private set { radius = value; } } //неважный код public Sphere(int r, double d) : base(d) { this.radius = r; } } 
  • Please provide a code to illustrate the problem. - Nikita
  • This is the call to Sphere sph = (Sphere) Global.figures [number - 1]; - Nerd0_0
  • most likely, you specify the name of the property instead of a field. You can use auto-properties: public SomeType SomeName{get;set;} - user227049
  • one
    Give a complete code example. Class with fields and call handling. - Denis Bubnov
  • 2
    @ Hope Do not add code in the comments. Instead, use the edit link to edit the question. - PetSerAl

1 answer 1

You have code:

 int radius { get { return radius; } private set { radius = value; } } 

during assignment, you call a closed loop, from which it is impossible to exit (turn on the debug and press F11 to see what the computer is doing)

need to write

 private int _radius; public int Radius { get { return _radius; } private set { _radius = value; } } 

and everything will be fine.

As another option, it is enough to write:

 public int radius { get; private set;}