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:
public static result-type operator Unary-operator (op-type operand)
public static result-type operator Binary-operator ( op-type operand, op-type2 operand2 )
public static implicit operator conv-type-out (conv-type-in operand)
public static explicit operator conv-type-out (conv-type-in operand)
Parameters :
- The result type of the result-type operator.
- Unary-operator one of the following operators: +-! ~ ++-true False
- Op-type the type of the first (or only) parameter.
- Operand the name of the first (or only) parameter.
- Binary-operator One: +-*/% & | ^ << >> = = = > < >= <=
- Op-type2 the type of the second parameter.
- Operand2 the name of the second parameter.
- The target type of the Conv-type-out type conversion operator.
- The input type of the conv-type-in type conversion operator.
Note :
- 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.
- 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).
- Operators can only take value parameters and cannot take ref or out parameters.
- 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.
- 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.
- 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.
- 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 :
- Target_type Target Type
- The Source_type source type.
- Identifier Something.
Note :
- 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 :
- 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