C # (2): Operator Overloading

Source: Internet
Author: User
Operator Overloading simplifies operations and makes operations more intuitive. However, compared with C ++, there are fewer operator sets that can be reloaded (it is strange that C ++ primer uses dozens of pages to discuss Operator overloading, which is a little too small. Why does this C # book only use one page ?). The following table lists the C # operators and their overloaded features.
Operator Overload features
+ -! ~ ++ -- True False Unary operators can be overloaded.
+-*/# & | ^ <> Binary operators can be reloaded.
= 1 = <> <=> = Comparison operators can be overloaded.
& | Cannot be overloaded
[] () Cannot be overloaded
+ =-= Etc. Cannot be overloaded
= .?; -> New is as sizeof typeof Cannot be overloaded

Note the following points:

    1. & | it cannot be directly overloaded, but it can be reloaded when it enters the calculation.
    2. [] cannot be overloaded. You can use the indexer to complete the required operations.
    3. () cannot be overloaded. Instead, the new conversion operator is defined.
    4. Compound operators cannot be overloaded because they are always broken down, for example, + =
    5. logical operators must be reloaded in pairs = and! =

below is a small example:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> Public static currency operator + (currency LHS, currency RHs)
{< br> return ( New currency (LHS. val + RHS. val);
}

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.