Introduction to all aspects of Operator Overloading in C #

Source: Internet
Author: User


1. What is Operator overload?

 

Assign the custom type to the operator number so that the object and object can be operated.

 

2. Keyword used: Operator

 

For example:

// Reload the unary operator prototype Declaration

Public static return-type operator OP (type operand)

{

......

}

 

// Reload binary operator prototype Declaration

Public static return-type operator OP (type operand1, type operand2)

{

......

}

 

3. Why do I need to overload operators?

 

I want to explain this to you in vernacular.

For example, you know that 1 + 1 = 2, and the computer knows that the operator + used in this case does not need to be reloaded. however, computers like this complex computing formula (x + y) ^ 2 won't understand it. At this time, ^ needs to be reloaded to tell the computer that the subalgorithm of this formula is: x * x + 2 * x * Y + y * Y. In fact, it is to replace an operator that the computer cannot recognize with an operator that the computer can recognize.

 

.

4. What are the overload operators?

 

Unary operator

!, ~, ++, ---, True, false

Binary Operators

+,-*,/, %, &, |, ^, <,>

Relational operators

= ,! =, <,>, <=,> =

 

5. Which operators cannot be overloaded include?

 

Conditional Operators

&, |

Array Operators

[]

Conversion Operators

()

Value assignment operator

+ =,-=, * =,/=, % =, & =, | =, ^ =, <=,> =

Others

= ,.,? :,->, New, is, sizeof, typeof

 

6. What are the advantages of Operator Overloading?

 

A) makes the code easier to understand and read.

B) You can use the priority relationship rules of existing operators to process operations between objects.

C) Make the code more flexible and the object operability better.

D) developers can draw a line from each other, because they are familiar with the regular value type operators, so that they can directly introduce these rules to custom objects.

 

7. C # What are the requirements for operator overloading?

 

1. all operators must be declared as public and static when being overloaded, indicating they are associated with their classes or structures, rather than instances, therefore, codes overloaded by operators cannot access non-static class members or this identifiers,

For more information about this, see:

Http://blog.csdn.net/cjr15233661143/article/details/8546946

 

2. Operators are easy to use, but cannot be overloaded at will. They must comply with the context and business background.


3. For comparison operators = ,! =, <,>, <=, >=. Duplicate loads are required when you reload the operator. For example, if you reload =, you must reload it! =

 

8. What functions can be implemented by operator overloading?

 

  • Supports implicit type conversion and explicit type conversion.
  • Supports basic unary operators, such as negative, inverse, auto-increment, and auto-subtraction.
  • Supports basic binary operators, such as addition, subtraction, multiplication, and division.
  • Supports basic Relational operators, such as greater than, less than, equal to, and not equal.
  • More complex operators are implemented, such as [], (), and bitwise operations.

 

9. What is implicit type conversion and display type conversion?

 

(1) implicit type conversion

 

Implicit conversion refers to the default system conversion. In essence, the data type of small storage capacity is automatically converted to the Data Type of large storage capacity. There are several types:

 

A) from the sbyte type to the short, Int, long, float, double, or decimal type.

B) from the byte type to the short, ushort, Int, uint, long, ulong, float, double, or decimal type.

C) from the short type to the int, long, float, double, or decimal type.

D) from the ushort type to the int, uint, long, ulong, float, double, or decimal type.

E) from int type to long, float, double, or decimal type.

F) from the uint type to the long, ulong, float, double, or decimal type.

G) from the long type to the float, double, or decimal type.

H) from the ulong type to the float, double, or decimal type.

I) from char type to ushort, Int, uint, long, ulong, float, double, or decimal type.

J) from float type to double type.

 

(2 ).Explicit type conversion

 

Explicit type conversion, that is, forced type conversion. If the two ends of the value assignment operation do not follow the implicit type conversion rules, explicit type conversion is required.


For example:

Char A = '1 ';

String B = (string);

Int c = (INT);

 

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.