C # Reference Conversion keywords: operator, explicit and implicit

Source: Internet
Author: User

operator

The operator keyword is used to declare an operator in a class or struct declaration. An operator declaration can take one of the following four 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    )
    3. public static implicit operator conv-type-out (conv-type-in operand)
    4. public static explicit operator conv-type-out (conv-type-in operand)

Parameters :

    1. The result type of the result-type operator.
    2. Unary-operator one of the following operators: +-! ~ ++-true False
    3. Op-type the type of the first (or only) parameter.
    4. Operand the name of the first (or only) parameter.
    5. Binary-operator One: +-*/% & | ^ << >> = = = > < >= <=
    6. Op-type2 the type of the second parameter.
    7. Operand2 the name of the second parameter.
    8. The target type of the Conv-type-out type conversion operator.
    9. The input type of the conv-type-in type conversion operator.

Note :

    1. The first two forms declare operators of user-defined overloaded built-in operators. Not all built-in operators can be overloaded (see overloaded operators). At least one of Op-type and op-type2 must be a closed type (that is, the type to which the operator belongs, or a type that is understood as a custom ). For example, this prevents the redefinition of the integer addition operator.
    2. The latter two forms declare the conversion operator. There is exactly one in conv-type-in and conv-type-out that must be a enclosing type (that is, the conversion operator can only be converted from its enclosing type to some other type, or from another type to its enclosing type).
    3. Operators can only take value parameters and cannot take ref or out parameters.
    4. C # requires paired overloaded comparison operators. if = = is overloaded, you must also overload! =, otherwise a compilation error occurs. At the same time, the comparison operator must return a value of type bool, which is the fundamental difference from other arithmetic operators.
    5. C # does not allow overloading the = operator, but if overloading such as the + operator, the compiler automatically uses the + operator's overload to perform the operation of the + = operator.
    6. Operator overloading is actually a function overload. This process is done by the compiler by first invoking the corresponding operator function with the specified expression, then converting the operand to the argument of the operator function, and then determining the overload of the function to be called, based on the type of the argument.
    7. Any operator declaration can be preceded by an optional list of properties (C # Programming Guide).
Explicit

The explicit keyword is used to declare user-defined type conversion operators that must be invoked using casts.

Static explicit operator Target_type {source_type identifier}

Parameters :

    1. Target_type Target Type
    2. The Source_type source type.
    3. Identifier Something.

Note :

    1. The conversion operator converts the source type to the target type. The source type provides the conversion operator. Unlike implicit conversions, an explicit conversion operator must be invoked by casting. If the conversion operation can result in an exception or loss of information, it should be marked as explicit. This prevents the compiler from silently invoking conversion operations that may have unforeseen consequences.
Implicit

The implicit keyword is used to declare an implicit user-defined type conversion operator.

Static implicit operator Target_type {source_type identifier}
Note :
    1. Implicit conversions can improve the readability of your source code by eliminating unnecessary type conversions. However, because implicit conversions can occur without the programmer's designation, care must be taken to prevent unpleasant consequences. In general, implicit conversion operators should never throw exceptions and not lose information so that they can be used safely without the programmer's knowledge. If the conversion operator does not meet those conditions, it should be marked as explicit.

Example :

The following is a comprehensive example that briefly demonstrates usage. For more specific details, please refer to the MSDN Library.

//Keywords_operator.cs
/*
Console output:
R3 =¥48 yuan 6.6 cents
float F = 48.66
R4 =¥48 yuan 6.5 cents
*/

We'll find the R4 result is a penny less! This is because in:

UINT FEN = (UINT) (((F-yuan) * 100)% 10);

In this sentence, a rounding error occurred while converting float to uint (this is related to the computer's binary storage). To resolve this error, we can use static methods in the System.Convert class to handle numbers:

UINT FEN = Convert.touint32 (((F-yuan) * 100)% 10);

However, there are some performance losses with system.convert processing.

C # Reference Conversion keywords: operator, explicit and implicit

Related Article

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.