4.2 Overloading of C #-----------------------------operators------------------------------------------

Source: Internet
Author: User

operators are typically used for system-predefined data types. If you define an operator in a class, it is called an operator overload.

Operator overloading includes unary operator overloading and two-tuple operator overloading, as well as user-defined data type conversions.

If there is a complex complex class that overloads the unary operator "+ +", it can be written as:

1  Public Static operator ++ (Complex a)23{45  ....................................................... 6 7 }

If the two-dollar operator "+" overload can be written as:

1 public static Complex operator +(Complex A,complex b) 2 c11>{ 3 ......... 4 }

The unary operator has one argument, and the two-dollar operator has two arguments, and the start of the overloaded operator must be modified with public static. The operators that can be overloaded include:

Unary operator:+ 、-、! , ~, + + 、--、 true, False.

Binary operators: + 、-、 *,/,%, &, |, ^, <<, >>, = =,! =, >, <, >=, <=.

The following operators require simultaneous overloading and cannot overload only one of them:

Unary operators: True and false.

Binary operators: = = and! = > and < >= and <=.

 The function of the operator:

Operator overloading brings some convenience to some of the operations of a class, and the real-part addition of two complex numbers can be written as:

1  Public Static Double Add (Complex A,complex b) 2 3 {45return a.r+B.R67 }

Such a write is not concise enough, and the class's member modifier is not public when it can not be directly manipulated.

4.7 Implementation of operator overloading-------------------------------------->>>>>>>>>>>>>>> >>>

First create a new class: Complex.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceApplication277 {8     classComplex9     {Ten         DoubleR, v; One          PublicComplex (DoubleRDoublev) A         { -              This. R =R; -              This. v =v; the  -         } -         //Two-dollar operator "+" overload -          Public StaticComplexoperator+ (Complex A, Complex b) {return NewComplex (A.R + B.R, A.V +B.V); } +  -         //Unary Operator "-" Overload +          Public StaticComplexoperator-(Complex a) A         { at             return NewComplex (-A.R,-a.v); -  -         } -  -         //Unary operator "+ +" overload -          Public StaticComplexoperator++(Complex a) in         { -             Doubler = A.R +1; to             Doublev = a.v +1; +             return NewComplex (r,v); -  the         } *          Public voidPrint () $         {Panax NotoginsengConsole.Write (r+" + "+v+"i\n"); -         } the  +     } A}

It is then implemented in the main program: Test.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceApplication277 {8     classTest9     {Ten          Public Static voidMain () One         { AComplex A =NewComplex (3,4); -Complex B =NewComplex (5,6); -              theComplex C =-A; - c.print (); -              -Complex d = A +b; + d.print (); - a.print (); +  AComplex e = a++;//First Assignment after + + at a.print (); - e.print (); -  -Complex f = ++a;//First + + after assignment - a.print (); - f.print (); in         } -     } to}

  

In operator overloading, the return value often requires new (new) new complex object.

Another kind of operator overload type is a user-defined data type conversion that implements conversions between different types, including both display and implicit conversions.

In programming, you often need to convert one type to another. For example, to convert int to double, they are the types that the system has already predefined, and the compiler knows how to perform their conversions, and the specifics will be said later.

If the type they have is not the compiler's predefined type, the compiler will not know how to perform the conversion, and the workaround is to use a user-defined data type conversion.

If the conversion process does not cause an exception due to data loss, an implicit conversion is used. If the conversion process is likely to lose data, a display conversion is used.

The implicit type conversion is written as follows:

 

  

4.2 Overloading of C #-----------------------------operators------------------------------------------

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.