Comparison and sorting of objects (5): C # Operator Overloading

Source: Internet
Author: User
C #Operator overload It is a rare application and a fun thing.
We can use it to achieve this requirement:
When an order is added with another order, a new order is formed. The goods in the new order are the union of the two orders, and the total price is the sum of the total prices of the two orders.

See the following implementation
Make a secondary item first Public   Class Merchandise
{
Public   Int Count {Get;Set;}
Public   String Name {Get;Set;}
Public   Double Unitprice {Get;Set;}
} Then implement the order class Public   Class Order
{
Public   Double Totalprices {Get;Set;}
Public List < Merchandise > Merchadises {Get;Set;}

Public   Static Order Operator   + (Order a, order B)
{
Order neworder =   New Order ()
{
Totalprices=A. totalprices+B. totalprices
} ;
Neworder. merchadises. addrange (A. merchadises );
Neworder. merchadises. addrange (B. merchadises );
Return Neworder;
}
} The following is a client callCode
  Order O1 =   New Order {Totalprices= 500.0} ;
O1.merchadises. addrange (
New List < Merchandise >
{
New Merchandise {Name= "Soap", Count=100, Unitprice=3} ,
New Merchandise {Name= "Towel", Count=100, Unitprice=2}
}
);

Order O2 =   New Order {Totalprices= 8000.0} ;
O1.merchadises. addrange (
New List < Merchandise >
{
New Merchandise {Name= "TV", Count=5, Unitprice=1000} ,
New Merchandise {Name= "DVD", Count=5, Unitprice=600}
}
);

Order O3 = O1 + O2;

It can be seen that the operator overload looks similar to the standard static method declaration, but they use the keyword operator and the operator itself, rather than a method name. Now you can successfully use the + operator and this class.

Note:

1. To overload operators, you can add operators to the class (they must be static ).

2. Some operators have multiple purposes (for example, the-operator has two functions: one element and two elements). Therefore, we also specify the number of operations to be processed and the type of these operations.

3. In general, the type of the operand is the same as that of the class defining the operator, but the operator of the mixed type can also be processed.

Add msdn instructions

OperatorThe keyword is used to declare the operator in the class or structure declaration. The operator declaration can take one of the following four forms:

 
Public staticResult-typeOperatorUnary-Operator(Op-type operand) Public staticResult-typeOperatorBinary-Operator(Op-type operand,Op-type2 operandroid 2) Public static implicit OperatorConv-type-out(Conv-type-In operand) Public static explicit OperatorConv-type-out(Conv-type-In operand)
Parameters
Result-type

The result type of the operator.

Unary-Operator

One of the following operators: + -! ~ ++-True False

Op-type

The type of the first (or unique) parameter.

Operand

The name of the first (or unique) parameter.

Binary-Operator

One of them: +-*/% & | ^ <>>=! ==<>=<=

Op-type2

Type of the second parameter.

Operand2

The name of the second parameter.

Conv-type-out

The target type of the type conversion operator.

Conv-type-in

The input type of the type conversion operator.

Remarks

The first two forms declare User-Defined operators that reload built-in operators. Note that not all built-in operators can be overloaded (see overloaded operators ).Op-typeAndOp-type2At least one of them must be a closed type (that is, the type of the operator ). For example, this prevents the redefinition of integer addition operators.

The latter two forms declare the conversion operators.Conv-type-inAndConv-type-outThere must be a closed type (that is, the conversion operator can only convert from its closed type to another type, or from another type to its closed type ).

The operator can only use value parameters, rather than ref or out parameters.

An optional attribute list (C # programming guide) is available before any operator declaration.

 

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.