Click here to Skip to main content
15,076,130 members
Articles / Programming Languages / C# 3.5
Tip/Trick
Posted 2 Oct 2016

Stats

13.3K views
9 bookmarked

C# – Operator Overloading

Rate me:
Please Sign up or sign in to vote.
4.04/5 (11 votes)
2 Oct 2016CPOL2 min read
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:

C#
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.

C#
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.

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

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

C#
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.

C#
 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.

C#
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.

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

New Object has been created for multiplication of two objects.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shamim Uddin
Software Developer Amber Software Solution Ltd.
Bangladesh Bangladesh
Hi, I am Shamim Uddin.Working with Microsoft Technologies.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Farhad Reza10-Dec-16 7:38
MemberFarhad Reza10-Dec-16 7:38 
GeneralMy vote of 5 Pin
mahfuzbappybd3-Oct-16 7:35
professionalmahfuzbappybd3-Oct-16 7:35 
Suggestiona little suggestion... Pin
poji2-Oct-16 20:32
Memberpoji2-Oct-16 20:32 
GeneralRe: a little suggestion... Pin
Shamim Uddin2-Oct-16 22:00
professionalShamim Uddin2-Oct-16 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.