With Xiaojing reading CLR via C # (08)-Operator

Source: Internet
Author: User
With Xiaojing reading CLR via C # (08)-Operator

We are no stranger to operators, such as binary operators such as +,-, *, and %, and ++ ,! . However, for non-primitive types, we need to use some custom methods to use these operators. Today I will share with you my knowledge about Operator Overloading and conversion operators.

I. Operator Overloading Method

CLR does not know the operator. Operator Overloading is just a method for it. However, CLR specifies how the language exposes Operator overloading.Programming LanguageYou can decide whether to support Operator overloading.

Pay attention to the following when defining the operator overload method:

    • The CLR specification requires that the operator overload method must bePublic staticType.
    • C # requires that the operator overload method must have a parameter type that is the same as the type currently defined for this method. Otherwise, a compilation error occurs.

Operator overload instance

We use the non-primitive complex overload operator "+ ".

After being defined, complex, a non-primitive type, can be conveniently used for + operations.

SepecialnameMark

Ildaxm.exe checks the metadata of the complex class and finds an op_addition item. If other operators are overloaded, The specialname tag is automatically generated.

During compilation, whenCodeWhen the "+" operator appears, the compiler automatically checks the specialname mark of op_addition and checks the compatibility of parameter types.

The following two tables are the unary operators and binary operators that can be reloaded by C. It is easy to understand, and there is no need to remember.

Ii. Methods of conversion Operators

When two non-primitive instances need to convert each other, we need to define the conversion operator method.Conversion OperatorIt refers to the method of converting an object from one type to another type.

Two rules

    • CLR requires that the overload method of the conversion operator must be a public static method.
    • C # The parameter type and return type must be at least the same as the type of the definition conversion method.

In addition, to complete the conversion operation, we should define two types of methods in the type:

    • Instance public constructor. The parameter is an instance of the source type.
    • Public instance method without ParametersToxxx. This method converts an instance of the definition typeXxxType.

Conversion operator example

Defines conversion operators for rational (rational number) types to facilitate mutual conversion with int32 instances.

Implicit and explicit

In this example, we define two conversion operators: the implicit keyword is represented inSource codeExplicit transformation is not required. This method is called only when explicit transformation is performed using the explicit keyword. The operator keyword indicates that this method is a transformation operator.

Use ildasm.exe to view,

Two methods are added: op_explicit and op_implicit. Implicit conversion can be performed only when no precision is lost after conversion. If precision is lost, it can only be defined as explicit conversion.

After the definition is complete, we can easily call the transformation.

Static void main (string [] ARGs)
{
Rational R1 = 5; // implicit conversion
Int32 x = (int32) R1; // display Conversion
}

Reload

In the preceding example, a type is often converted to multiple instance types. In this case, you need to define other conversion operators, such as public static explicit operator single (rational R ), that is, method overloading. We are no stranger to overloading, except that the two explicit overload methods are differentiated only by different return value types.

CLR Allows defining multiple methods with the same name, as long as the parameter or return value type of each method is different. However, when C # judges the uniqueness of a method, only the parameter differences are considered except the method name, while the return type of the method is ignored. Therefore, the difference between the above two overload methods and only the return type is actually a special case of C #'s relaxation restrictions.

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.