Свойство и System.StackOverflowException - C#
Формулировка задачи:
Доброго времени суток! Простое задание на наследование. Всё вроде как понятно, но выпадает ошибка System.StackOverflowException. Пытался добавлять например в Square еще private int perimetr , и его уже записывать в свойство, но тогда значение у меня всегда 0. Подскажите, в чем проблема?
class Square
{
public int a;
public Square (int a)
{
this.a = a;
}
public int Perimetr
{
set
{
Perimetr = a * 2;
}
get
{
return Perimetr;
}
}
}
class Rectangle : Square
{
public int b;
public Rectangle(int a, int b)
:base (a)
{
this.b = b;
}
public new int Perimetr
{
get
{
return Perimetr;
}
set
{
Perimetr = (a * 2) + (b * 2);
}
}
}
class Demo
{
static void Main()
{
int tempA;
int tempB;
Console.WriteLine("Please enter figure side A");
tempA = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter figure side B");
tempB = Convert.ToInt32(Console.ReadLine());
if (tempA == tempB)
{
Square Sq = new Square(tempA);
Console.WriteLine("Square perimetr is: " + Sq.Perimetr);
}
else
{
Rectangle Re = new Rectangle(tempA, tempB);
Console.WriteLine("Rectangle perimetr is: " + Re.Perimetr);
}
Console.ReadLine();
}
}Решение задачи: «Свойство и System.StackOverflowException»
textual
Листинг программы
public int Perimetr
{
get
{
return a * 2;
}
}