C # operator overloading

Source: Internet
Author: User

One, C # operator overloading

1. C # operator overloading

Operator overloading refers to allowing users to write operational symbols with their own defined types, while also allowing user-defined types to have the same functionality as predefined types.

The purpose of the overloaded operators is to facilitate the use of our own classes or structs. All operator overloads are static methods of a class or struct.

Therefore, in the C # language, allow user-defined types to overload operators by using the operator keyword as a static member function. The operator keyword is used to declare an operator in a class or struct declaration.

2. Overloading of Operators

In C #, not all operators can be overloaded. The following table lists the overloads of the operators:

Note: If you overload the comparison operator, you must overload the "! =" overload, that is, if you overload "= =". The reverse is true. The same goes for "<" and ">" and "<=" and ">=".

3. Overloading a unary operator

A unary operator has only one parameter.

public static Return-type operator op (type operand)
{
Statement
}

4. Overloading the two-tuple operator

The binary operator has two parameters.

public static Return-type operator op (type operand1, type Operand2)
{
Statement
}

Second, prompt

C # operator overloads are declared in the same way as methods, but the operator keyword tells the compiler that it is actually an operator overload followed by the sign of the related operator, which is the type that was obtained when the operator was used.

C # requires that all C # operator overloads be declared public and static, which means that they are associated with their class or struct, not the instance, so the code body for operator overloading cannot access non-static members and cannot access the this identifier.

Iii. examples

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Test
{

public class ComplexNumber
{
private int real;
private int imaginary;
Public ComplexNumber (): This (0, 0)//constructor
{
}
Public ComplexNumber (int r, int i)//constructor
{
Real = R;
imaginary = i;
}
Override ToString () to display a complex number in the traditional format
public override string ToString ()
{
Return (System.String.Format ("{0} + {1}i", real, imaginary));
}
Overloading the "+" operator
public static ComplexNumber operator+ (ComplexNumber A, ComplexNumber b)
{
return new ComplexNumber (A.real + b.real, a.imaginary + b.imaginary);
}
Overloaded "'-" operator
public static ComplexNumber operator-(ComplexNumber A, ComplexNumber b)
{
return new ComplexNumber (A.real-b.real, a.imaginary-b.imaginary);
}
}

Class Program
{
static void Main (string[] args)
{
C # operator Overloading-www.baike369.com
ComplexNumber a = new ComplexNumber (10, 12);
ComplexNumber B = New ComplexNumber (8, 9);
Console.WriteLine ("Complex number A = {0}", a.tostring ());
Console.WriteLine ("Complex number B = {0}", b.tostring ());
ComplexNumber sum = a + b;
Console.WriteLine ("Complex number sum = {0}", Sum.) ToString ());
ComplexNumber difference = a-B;
Console.WriteLine ("Complex Number difference = {0}",
Difference. ToString ());
Console.ReadLine ();
}
}
}

Operation Result:

Complex number A = ten + 12i
Complex number B = 8 + 9i
Complex number sum = 21i
Complex number difference = 2 + 3i

C # operator overloading

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.