Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# – Operator Overloading

0.00/5 (No votes)
2 Oct 2016 1  
Operator overloading in C#

In this tip, we will discuss about operator overloading in C#.

Table of Contents

  • Problem/ Scenario
  • Background
  • The Complete list of Operators that can be overloaded
  • The operator which cannot be overloaded
  • Explanation about the code
  • Conclusion

Problem/ Scenario

We want to calculate area of square using Operator Overloading. Consider we have two different squares. The length and width of both squares are different. We have to calculate the total area of two squares.

square

Figure: Squares

Background

Operator overloading is very much like Method Overloading. All operator Overloading methods are static methods of the class. Operator will be used between objects of class. Here, I am going to use console application of Visual Studio.

The complete list of Operators that can be overloaded are:

  • Unary Operator : +,-,!,~,++,--,true,false
  • Binary Operator :+,-,*,/,%,&,|,^,<<,>>,==,!=,>,<,>=,<=

The operators which cannot be overloaded are:

=,&&,||,?:,Cheeked, new, typeof, as, and is operator.

Now, we will create a Square class. Below are the following codes:

public class Square
    {
        public decimal Length { set; get; }
        public decimal Width { set; get; }

        public static Square operator *(Square _squareObj1, Square _squareObj2)
        {
            Square _squareObj = new Square();
            _squareObj.Length = _squareObj1.Length * _squareObj2.Length;
            _squareObj.Width = _squareObj1.Width * _squareObj2.Width;
            return _squareObj;
        }

        public decimal Area()
        {
            return Length * Width;
        }
    }

Let’s get an explanation about the code:

public class Square

Here, square is the class name.

public decimal Length { set; get; }
public decimal Width { set; get; }

There are two properties of Square class. One is Length which has decimal data type. And the other is width which has a decimal type.

public static Square operator *(Square _squareObj1, Square _squareObj2)
{
    Square _squareObj = new Square();
    _squareObj.Length = _squareObj1.Length * _squareObj2.Length;
    _squareObj.Width = _squareObj1.Width * _squareObj2.Width;
    return _squareObj;
}

The above method implements the Multiplication operator (*) for a user-defined class Square. This method is a static method which has two parameters. Both are Square’s class objects and multiplication is done on both objects.

public decimal Area()
{
    return Length * Width;
}

We have Multiplication Length and Width. Now, we will call Square class in Main function:

class Program
    {
        static void Main(string[] args)
        {
            // First Square 
            Square _obj1 = new Square();
            _obj1.Length = 40;
            _obj1.Width = 40;

            // Second Square 
            Square _obj2 = new Square();
            _obj2.Length = 50;
            _obj2.Width = 50;

            // For match tow Object 
            Square _obj3 = new Square();
            _obj3 = _obj1 * _obj2;

            // Assign area to variable
            decimal area = _obj3.Area();
            Console.WriteLine(area);
            Console.ReadKey();
        }
    }

Let’s get an explanation about the code: We have calculated the total area of two squares.

 Square _obj1 = new Square();
_obj1.Length = 40;
 _obj1.Width = 40;

We have created an object with name given _obje1. We have assigned length of square as 40 and width of square as 40.

Square _obj2 = new Square();
 _obj2.Length = 50;
 _obj2.Width = 50;

Again, we have created an object whose name is given as _obje2. We have assigned length of square as 50 and other width of square as 50.

Square _obj3 = new Square();
 _obj3 = _obj1 * _obj2;

New Object has been created for multiplication of two objects.

decimal area = _obj3.Area();
Console.WriteLine(area);
Console.ReadKey();

The Area method has been called and assigned result to area variable.

34324

Conclusion

Now we are done as we have calculated the amount of two Squares. The above picture shows the result of amount.

Hope this will be helpful. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here