Page 1 of 1

Operator Overloading

#1 sparkart  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 691
  • Joined: 16-February 09

Posted 23 August 2009 - 12:26 PM

Operator Overloading

C# operators use predefined types as operands. But if a user-defined type (such as a class) is used as an operand, the operator would not know how to process it. Operator overloading allows you to define how the C# operators should operate on your user-defined types.

Operator overloading is only available for classes and structs. You overload an operator by defining a method named: operator x where x is the operator you wish to overload (e.g. operator +, operator -, operator *, etc.). Overload methods for unary operators require a single parameter of the class/struct type. Overload methods for binary operators require two parameters with atleast one parameter with the class/struct type. The overload method must also contain the public and static modifiers.

For example:
//Unary operator
public static myClass operator +(myClass x) { ... }

//Binary operator
public static myClass operator -(myClass x, int y) { ... }

Not all operators can be overloaded the following are a list of overloadable operators:
Unary operators: +, -, !, ~, ++, --, true, false
Binary operators: +, -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <=

Notice that the assignment operator (=) cannot be overloaded.

Operator overloading does not allow:
- the creation of a new operator.
- redefinition of its workings on pre-defined types.
- precedence/associativity change of the operator.

Let's get into some examples to get a deeper understanding on how operator overloading would be quite useful. Let's start with our class example:

class Vector
	int x = 0, y = 0, z = 0;

Using our Vector class in conjunction with any operators would be a problem since operators do not know how to operate on user-defined types (such as our Vector class):
Vector v1 = new Vector();
Vector v2 = new Vector();

// The + operator does not know how to process "Vector" type.
v1 = v1 + v2;

To remedy this, we overload the + operator and define the way it operates with our Vector class:
public static Vector operator +(Vector a, Vector b)
	Vector c = new Vector();
	c.x = a.x + b.x;
	c.y = a.y + b.y;
	c.z = a.z + b.z;

	return c;

Now we can use the + operator to add two Vector types together:
v1 = v1 + v2;

In the previous example, v1 + v2 v1 is the first parameter and v2 is the second parameter.

The first thing that happens is a new Vector object is created (which will be used to store the results of the addition). v1.x is added with v2.x and stored in the new Vector object. The same thing happens for y and z. After adding all three fields of both operands, the results are returned.

Operator overloading is simply used to make our user-defined types compatible with operators. Please be cautious as to how you implement the overloaded operators. They should conform to the intuitive meanings of the operators. For instance, it would be unwise to overload the + operator to perform some sort of division.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1