Get the App
SLTechnology News&Howtos  ›  Servers  › 

Operator overloading

Shulou Source: shulou.com Published: 2022-06-03 03:03:50 10月20日 Update

1. Specific explanation

You can redefine or overload the operators built into C #. Therefore, programmers can also use operators of user-defined types. An overloaded operator is a function with a special name, defined by the symbol of the operator followed by the keyword operator. Like other functions, overloaded operators have return types and parameter lists. Note that it must be decorated with public and must be a static method of the class. But not all built-in operators can be overloaded.

two。 Operators that can be overloaded and cannot be overloaded

The unary operators of +, -,!, ~, +,-- true false can be overloaded. True false must be overloaded in pairs

+, -, *, /,% these binary operators can be overloaded.

These comparison operators can be overloaded. Must be overloaded

&, | | these conditional logical operators cannot be directly overloaded.

+ =,-=, * =, / =,% = these assignment operators cannot be overloaded.

=,.,?:,->, new, is, sizeof, typeof operators cannot be overloaded.

3. Grammar

The operator keyword is used to declare operators in a class or structure declaration. There are two main forms:

"1" public static result-type operator unary-operator (op-type operand)

"2" public static result-type operator binary-operator (op-type operand, op-type2 operand2)

Note:

Tags: Operator operation parameter type key keyword box item function addition instance form note user symbol reference special two code right Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Apple MySQL Shulou Tech Info