5
The code in question is used to calculate the circle area value using keywords get
and set
.
After I occurred CS1729 error a constructor was created to fix it
private double valor;
public CirculoApp(double valor)
{
this.valor = valor;
}
When I run the code the following output is shown:
Digite o raio do círculo 1: 5
Digite o raio do círculo 2: 6
Digite o raio do círculo 3: 7
Process is terminated due to StackOverflowException.
Error appears on line 14:
public double Raio
{
get => Raio; X
set => Raio = value;
}
X = System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.'
Here is the code.
using static System.Console;
public class CirculoApp
{
private double valor;
public CirculoApp(double valor)
{
this.valor = valor;
}
public double Raio
{
get => Raio;
set => Raio = value;
}
public double Area
{
get => Area;
set => Area = 3.14159 * Raio * Raio;
}
public string toString() => Raio + " " + Area + " ";
}
public class PropCirculoApp
{
static int Main(string[] args)
{
Write("Digite o raio do círculo 1: ");
if (!double.TryParse(ReadLine(), out var valor)) return 1;
var circulo1 = new CirculoApp(valor);
Write("Digite o raio do círculo 2: ");
if (!double.TryParse(ReadLine(), out valor)) return 1;
var circulo2 = new CirculoApp(valor);
Write("Digite o raio do círculo 3: ");
if (!double.TryParse(ReadLine(), out valor)) return 1;
var circulo3 = new CirculoApp(valor);
WriteLine(circulo1.toString());
WriteLine(circulo2.toString());
WriteLine(circulo3.toString());
ReadKey();
return 0;
}
}
I gave you the right way, simple and correct: https://answall.com/a/413057/101. The error only happened because you decided to change the wrong way. Not only is it not wrong, it’s simpler.
– Maniero
Possible duplicate of Circle area application: method shows that not all code paths return value
– Augusto Vasques