C # Operator Overloading

Source: Internet
Author: User

All unary and binary operators have pre-defined implementations, that are automatically available in any expressions. In addition to this pre-defined implementations, user defined implementations can also be introduced in C #.
Mechanisms of giving a special meaning to a standard C # operator with respect to a user defined data type such as classes or structures is known as operator overloading. remember that it is not possible to overload all operators in C #. the following table shows
The operators and their overloadability in C #.

Operators overloadability

+,-, *,/, %, &, |, <,> All C # binary operators can be overloaded.

+ ,-,!, ~, ++,-, True, false all C # unary operators can be overloaded.

= ,! =, <,>, <=,> = All Relational operators can be overloaded,
But only as pairs.

&, | They can't be overloaded

() (Conversion operator) They can't be overloaded

+ =,-=, * =,/=, % = These compound assignment operators can be
Overloaded. But in C #, these operators are
Automatically overloaded when the respective
Binary operator is overloaded.

= ,.,? :,->, New, is, as, size of these operators can't be overloaded

In C #, a special function called operator function is used for overloading purpose. These special function or method must be public and static.
They can take only value arguments. The ref and out parameters are not allowed as arguments to operator functions.
The general form of an operator function is as follows.

Public static return_type operator OP (argument list)
Where the op is the operator to be overloaded and operator is the required keyword.For overloading the unary operators, there is only one argument and for overloading a binary operator there are two arguments. Remember that at least one
The arguments must be a user-defined type such as class or struct type.

Overloading unary operators-the general form of operator function for unary operators is as follows. public static return_type operator OP (type T) {// statements} Where type must be a class or struct. the return type can be any type
Authorization t void for unary operators like + ,~, ! And dot (.). but the return type must be the type of 'type' for ++ and O remember that the true and false operators can be overloaded only as pairs. the compilation error occurs if a class declares one of these operators
Without declaring the other.

The following program overloads the Unary-operator inside the class Complex

// Unary operator overloading

Using system;

Class Complex
{
Private int X;
Private int y;
Public complex ()
{
}
Public complex (int I, Int J)
{
X = I;
Y = J;
}

Public void showxy ()
{
Console. writeline (/"{0} {1}/", x, y );
}

Public static complex operator-(Complex C)
{
Complex temp = new complex ();
Temp. x =-C. X;
Temp. Y =-C. Y;
Return temp;
}
}

Class myclient
{
Public static void main ()
{
Complex C1 = new complex (10, 20 );
C1.showxy (); // displays 10 & 20
Complex C2 = new complex ();
C2.showxy (); // displays 0 & 0
C2 =-C1;
C2.showxy (); // diapls-10 &-20
}
}

Overloading binary Operators

An overloaded binary operator must take two arguments, at least one of them must be of the type class or struct, inwhich the operation is defined. but overloaded binary operators can return any value before t the type void. the General
Form of a overloaded binary operator is as follows.

Public static return_type operator OP (type1 T1, type2 T2)
{
// Statements
}

A concrete example is given below

// Binary operator overloading

Using system;

Class Complex
{
Private int X;
Private int y;
Public complex ()
{
}
Public complex (int I, Int J)
{
X = I;
Y = J;
}

Public void showxy ()
{
Console. writeline (/"{0} {1}/", x, y );
}

Public static complex operator + (complex C1, complex C2)
{
Complex temp = new complex ();
Temp. x = c1.x + c2.x;
Temp. Y = c1.y + c2.y;
Return temp;
}
}

Class myclient
{
Public static void main ()
{
Complex C1 = new complex (10, 20 );
C1.showxy (); // displays 10 & 20
Complex C2 = new complex (20, 30 );
C2.showxy (); // displays 20 & 30
Complex C3 = new complex ();
C3 = C1 + C2;
C3.showxy (); // dislplays 30 & 50
}
}

The binary operators such as = ,! ==,<>,<=, >= Can be overloaded only as pairs. Remember that when a binary arithmetic operator is overloaded, corresponding assignment operators also get overloaded automatically. For example
If we overload + operator, it implicitly overloads the + = operator also.

Summary

1. the user defined operator declarations can't modify the syntax, precedence or associatiator of an operator. for example, a + operator is always a binary operator having a predefined precedence and an associativity of left to right.
2. user defined operator implementations are given preference over predefined implementations.
3. Operator overload methods can't return void.
4. the operator overload methods can be overloaded just like any other methods in C #. the overloaded Methods shocould differ in their type of arguments and/or number of arguments and/or order of arguments. remember that in this case also the return type is not
Considered as part of the method signature.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.