Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 14 Expressionsprevious at this levelnext at this level 14.2 Operatorsprevious at this levelnext at this level 14.2.2 Operator overloading Paragraph 11 All unary and binary operators have predefined implementations that are automatically available in any expression. 2 In addition to the predefined implementations, user-defined implementations can be introduced by including operator declarations in classes and structs (§17.9). 3 User-defined operator implementations always take precedence over predefined operator implementations: Only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered. Paragraph 21 The overloadable unary operators are:
+   -   !   ~   ++   --   true   false  
[Note: Although true and false are not used explicitly in expressions, they are considered operators because they are invoked in several expression contexts: boolean expressions (§14.16) and expressions involving the conditional (§14.12), and conditional logical operators (§14.11). end note] 2 The overloadable binary operators are:
+   -   *   /   %   &   |   ^   <<   >>   ==   !=   >   <   >=   <=  
Paragraph 31 Only the operators listed above can be overloaded. 2 In particular, it is not possible to overload member access, method invocation, or the =, &&, ||, ?:, checked, unchecked, new, typeof, as, and is operators. Paragraph 41 When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded. 2 For example, an overload of operator * is also an overload of operator *=. 3 This is described further in §14.13. 4 Note that the assignment operator itself (=) cannot be overloaded. 5 An assignment always performs a simple bit-wise copy of a value into a variable. Paragraph 51 Cast operations, such as (T)x, are overloaded by providing user-defined conversions (§13.4). Paragraph 61 Element access, such as a[x], is not considered an overloadable operator. 2 Instead, user-defined indexing is supported through indexers (§17.8). Paragraph 71 In expressions, operators are referenced using operator notation, and in declarations, operators are referenced using functional notation. 2 The following table shows the relationship between operator and functional notations for unary and binary operators. 3 In the first entry, op denotes any overloadable unary prefix operator. 4 In the second entry, op denotes the unary postfix ++ and --operators. 5 In the third entry, op denotes any overloadable binary operator. [Note: For an example of overloading the ++ and --operators see §17.9.1. end note]
Operator notation Functional notation
op x operator op(x)
x op operator op(x)
x op y operator op(x,y)
Paragraph 81 User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration. [Note: Thus, it is not possible for a user-defined operator to have the same signature as a predefined operator. end note] Paragraph 91 User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator. [Example: For example, the / operator is always a binary operator, always has the precedence level specified in §14.2.1, and is always left-associative. end example] [Note: While it is possible for a user-defined operator to perform any computation it pleases, implementations that produce results other than those that are intuitively expected are strongly discouraged. For example, an implementation of operator == should compare the two operands for equality and return an appropriate bool result. end note] Paragraph 101 The descriptions of individual operators in §14.5 through §14.13 specify the predefined implementations of the operators and any additional rules that apply to each operator. 2 The descriptions make use of the terms unary operator overload resolution, binary operator overload resolution, and numeric promotion, definitions of which are found in the following sections.
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS