CSharp - Overload unary operator ++ to a rectangle object to increment the length and breadth of a rectangle

Introduction

The following code overloads the unary operator ++ to a rectangle object to increment the length and breadth of the rectangle object.

Demo

using System;
class Rectangle//from  w  ww. j a v a2s  .  com
{
    public double length, breadth;
    public Rectangle(double length, double breadth)
    {
        this.length = length;
        this.breadth = breadth;
    }
    public double AreaOfRectangle()
    {
        return length * breadth;
    }
    public static Rectangle operator ++(Rectangle rect)
    {
        rect.length++;
        rect.breadth++;
        return rect;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle(5, 7);
        Console.WriteLine("Length={0} Unit Breadth={1} Unit", rect.length, rect.breadth);
        Console.WriteLine("Area of Rectangle={0} Sq. Unit", rect.AreaOfRectangle());
        rect++;
        Console.WriteLine("Modified Length={0} Unit Breadth={1} Unit", rect.length, rect.breadth);
        Console.WriteLine("Area of new Rectangle={0} Sq. Unit", rect.AreaOfRectangle());
    }
}

Result

Related Topic