C # Operator re-Attention

Source: Internet
Author: User
Operator Overloading is the ability to allow users to write expressions using user-defined types.
For example Code To add two numbers. Obviously, sum is the sum of two numbers.
Int I = 5;
Int sum = I + J;
If you can use a user-defined type that represents a complex number to write expressions of the same type, it is best:
Complex I = 5;
Complex sum = I + J;
Operator Overloading allows you to specify operators such as "+" for user-defined type overloading. If you do not need to reload, you need to write the following code:
Complex I = new complex (5 );
Complex sum = complex. Add (I, j );
This code can run well, but the complex type does not work as well as the predefined type in the language.

Auto: http://www.ixpub.net/archiver/tid-677435.html

In my opinion, the operator re-attention enables struct, class, interface, and so on to perform computation.

For example, when the operator is repeated:
First, the names are public and static, followed by the return type, followed by the operator, and followed by the operators to be clarified, finally, add the exact sequence number to the corresponding sequence.
In the following exampleStructIn hourPublic StaticHourOperator+(Hour LHS, hour RHs ){...}


Operator re-Attention method:
1. the distinct operator re-Attention method.
2. After the examples are completed, run the following operators:
The following example uses the token to describe: Class Program
{
Static   Void Main ( String [] ARGs)
{
Hour hrvalue1 =   New Hour ( 10 );
Hour hrvalue2 =   New Hour ( 20 );

// 2. get the result of adding two hour values.
hour hrsum = hrvalue1 + hrvalue2;

//Returns the result of hour + Int.
Hour hrsumint=Hrvalue1+ 10;

// Returns the result of int + hour.
Hour hrintsum =   11   + Hrvalue2;
Console. writeline ( " Hrvalue1 + hrvalue2 = {0} " , Hrsum. ivalue );
Console. writeline ( " Hrvalue1 + 10 = {0} " , Hrsumint. ivalue );
Console. writeline ( " 11 + hrvalue2 = {0} " , Hrintsum. ivalue );
Console. readkey ();
}
Struct Hour
{
Private   Int Value;
// This component creates hour class for the root letter int Value
Public Hour ( Int Ivalue)
{
This . Value = Ivalue;
}
// Define a property so that the value can be used.
Public   Int Ivalue
{
Get
{
Return Value;
}
}
//1, Declare a binary operator and add two hour classes together
Public   Static Hour Operator + (Hour LHS, hour RHs)
{
Return   New Hour (LHS. Value + RHS. value );
}

/*
The operator is public. All operators must be public.
The operator is static. All operators must be static, and operations never have the multi-Attention property,
And cannot use virtual, abstract, override, or sealed modifier.
The binary operator (for example, +) has two linear numbers. The unary operator has a linear number.

We have the public hour (INT ivalue) structure to add an int to hour, but first convert the int into hour
Hour A =;
Int B =;
Hour sum = a + new hour (B );
Although the above generation is completely valid, it is not clear or accurate to allow a hour and an int to be directly added.
To enable hour to + int, a binary operator + must be specified. The first vertex is hour, and the second vertex is an int.
*/
Public   Static Hour Operator + (Hour LHS, Int RHS)
{
Return LHS +   New Hour (RHs );
}
// Make an int + hour.
Public   Static Hour Operator   + ( Int LHS, hour RHs)
{
Return   New Hour (LHS) + RHS;
}
}
}

Example 2: Struct Hour
{
Public   Int Ivalue;
// Structured Data
Public Hour ( Int Initialvalue)
{
This . Ivalue = Initialvalue;
}
// 1,Scalar operator)
Public   Static Hour Operator   ++ (Hour huvalue)
{
Huvalue. ivalue ++ ;
Return Huvalue;
}

// = The operator is a binary operator and must contain two numbers.
Public   Static   Bool   Operator = (Hour LHS, hour RHs)
{
Return LHS. ivalue = RHS. ivalue;
}
Public   Static   Bool   Operator ! = (Hour LHS, hour RHs)
{
Return LHS. ivalue ! = RHS. ivalue;
}
}

// ++ Operators are in the form of prefix and suffix. C # can intelligently use the same operator for prefix and suffix.
Static   Void Main ( String [] ARGs)
{
// 2,Case-based
Hour Hu =   New Hour ( 10 );
// 3,
// Operator re-Attention
Hu ++ ;
// Returns the result after the deduplication.
Console. writeline ( " Hu ++ = {0} " , Hu. ivalue );
// Operator re-Attention
++ Hu;
// Returns the result after the deduplication.
Console. writeline ( " Hu ++ = {0} " , Hu. ivalue );
Console. readkey ();
}

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.