abstract class Shape
{
public abstract double GetArea();
}
class Circle : Shape
{
public double radius { get; set; }
public override double GetArea()
{
return Math.PI * radius * radius;
}
}
class Rectangle : Shape
{
public double width { get; set; }
public double height { get; set; }
public override double GetArea()
{
return width * height;
}
}
class Triangle : Shape
{
public double baseLength { get; set; }
public double height { get; set; }
public override double GetArea()
{
return 0.5 * baseLength * height;
}
}
class Program
{
static void Main()
{
Shape[] shapes = new Shape[]
{
new Circle { radius = 5 },
new Rectangle { width = 4, height = 6 },
new Triangle { baseLength = 3, height = 8 }
};
foreach (var shape in shapes)
{
double area = shape switch
{
Circle circle => circle.GetArea(),
Rectangle rectangle => rectangle.GetArea(),
Triangle triangle => triangle.GetArea(),
_ => throw new ArgumentException("Unsupported shape type")
};
Console.WriteLine($"The area of the {shape.GetType().Name} is {area}");
}
}
}