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.
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)
{
Square _obj1 = new Square();
_obj1.Length = 40;
_obj1.Width = 40;
Square _obj2 = new Square();
_obj2.Length = 50;
_obj2.Width = 50;
Square _obj3 = new Square();
_obj3 = _obj1 * _obj2;
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.
Conclusion
Now we are done as we have calculated the amount of two Square
s. The above picture shows the result of amount.
Hope this will be helpful. :)