This article introduces a brief introduction to the operators in asp.net. If you need them, please refer to the introduction below.
Operator Overloading in c:
The Code is as follows: |
Copy code |
Class Test { Public static Test operator + (Test arg1, Test arg2 ){...} } |
Although CLR knows nothing about operators, it regulates how programming languages provide Operator Overloading so that they can be easily used by code written in different programming languages, each programming language decides whether to support Operator overloading, and if it provides, expresses, and uses their syntax, for CLR, Operator Overloading is just some method.
When editing the code, the editor generates a method definition named op_Addition. The method definition entry has a specialname mark, indicating that this is a "special" method. When the Editor (including the C # Editor) sees the + operators in the source code, they will see which of the operand types in the editor defines the specialname method with the parameter type compatible with the operand type and named op_Addition. If such a method exists, the compiler calls the method. If the method does not exist, a compilation error occurs.
Operator and language interoperability:
Operator Overloading is a very useful tool. It can express its own ideas through concise syntax. However, not many programming languages Support Operator overloading, such as Visual Basic. Therefore, When Visual Basic developers perform operations on a non-primitive type) when the + operator is applied, the compiler will generate an error and stop running. This creates a problem. How can a developer who uses languages that do not support Operator Overloading call another language definition type that supports Operator Overloading?
Take Visual Basic and C # as an example (Visual Basic does not support Operator overloading, while c # Does ):
1. There is no operator overload in Visual Basic, but an op_Addition (equivalent to +) method is provided;
First, create a vb class library and create a VbType class. The class content is as follows:
The Code is as follows: |
Copy code |
Public Class VbType Public Shared Function op_Addition (ByVal a As VbType, ByVal B As VbType) As String Return "operator +" End Function End Class |
2. Perform + operations on the above Type of instance in c #
The Code is as follows: |
Copy code |
Using System; Class Program { Static void Main (string [] args) { // This operation is incorrect. // Compilation fails. Console. WriteLine (vb + vb ); // You can only access Console. WriteLine (VbType. VbType. op_Addition (vb, vb )); } } |
The reason for not using + to operate two instances is: when the c # compiler encounters +, it will go to the metadata to find the op_Addition method with the specialname metadata tag, the compiler can determine that this is an operator overload method, but the op_Addition method generated by vb does not have the specialname flag. Therefore, the compiler reports an error.
On the contrary, if the overload of A + operator is defined in c # And the instance + operation is performed in vb, The op_Addition method can only be called.