wasAsked.

Write code to calculate the area of a rectangle using an interface.

PwC · Role not specified · technical

Published

Asked in 2026 · C#

Community answers

Posted by wasAsked editorial collection

Define the capability in an interface and keep rectangle validation in the implementation. Example: public interface IShape { decimal Area { get; } } public sealed record Rectangle : IShape { public Rectangle(decimal width, decimal height) { if (width <= 0 || height <= 0) throw new ArgumentOutOfRangeException(); Width = width; Height = height; } public decimal Width { get; } public decimal Height { get; } public decimal Area => Width * Height; } IShape shape = new Rectangle(5m, 3m); Console.WriteLine(shape.Area); // 15 The interface lets consumers work with different shapes without knowing concrete types.

Backbencher Developer · · 0 likes

public interface IShape { string Name { get; } double Area(); double Perimeter(); } public sealed class Rectangle : IShape { public double Width { get; } public double Height { get; } public Rectangle(double width, double height) { if (width <= 0) throw new ArgumentOutOfRangeException(nameof(width), "Width must be positive."); if (height <= 0) throw new ArgumentOutOfRangeException(nameof(height), "Height must be positive."); Width = width; Height = height; } public string Name => "Rectangle"; public double Area() => Width * Height; public double Perimeter() => 2 * (Width + Height); } // Adding another shape requires no change to existing code (Open/Closed Principle) public sealed class Circle(double radius) : IShape { public string Name => "Circle"; public double Area() => Math.PI * radius * radius; public double Perimeter() => 2 * Math.PI * radius; } public static class Program { public static void Main() { IShape rectangle = new Rectangle(5, 4); Console.WriteLine($"{rectangle.Name} area: {rectangle.Area()}"); // Rectangle area: 20 List<IShape> shapes = [new Rectangle(3, 2), new Circle(1)]; foreach (var shape in shapes) Console.WriteLine($"{shape.Name}: area {shape.Area():F2}, perimeter {shape.Perimeter():F2}"); } } Points to explain - The interface defines the contract (what a shape can do); Rectangle provides the implementation. - Client code depends on IShape, so it works with any shape through polymorphism. - Validation in the constructor keeps the object in a valid state; properties without setters make it immutable. - Using the interface makes unit testing and extension easy — for example a service that calculates total floor area can accept IEnumerable<IShape>. Possible follow-ups - Square: create a separate class implementing IShape rather than inheriting from Rectangle, to avoid the classic Liskov Substitution problem. - Use decimal for exact measurements such as money, or double for geometry. - A record version: public sealed record Rectangle(double Width, double Height) : IShape { ... } gives value equality for free. - Default interface method: IShape could provide string Describe() => $"{Name} with area {Area():F2}";

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics