Tuesday, April 14, 2015

C# - Operator Overloading

In an object oriented programming language like C#, operator overloading provides a much more natural way of implementing the operations on custom types.Suppose that we have a class created for Complex number and we want to perform all the arithmetic operations on this type. One way to do this is by having functions like Add, Subtract inside the class and have the functionality. Another way is to actually have the overloaded version of operators to act on this type.



Operator overloading provides a much natural abstraction for the types. When we think about possible operation on some data type we can think of binary operators, unary operators, relational operators and perhaps some conversion operations to and from the basic types. In C# achieving all this is possible using operator overloading.

When the above code is compiled and executed, it produces the following result:

references: 

No comments:

Post a Comment