Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / docs / ecma334 / 17.9.1.xml
1 <?xml version="1.0"?>
2 <clause number="17.9.1" title="Unary operators">
3   <paragraph>The following rules apply to unary operator declarations, where T denotes the class or struct type that contains the operator declaration: <list><list_item> A unary +, -, !, or ~ operator must take a single parameter of type T and can return any type. </list_item><list_item> A unary ++ or  --operator must take a single parameter of type T and must return type T. </list_item><list_item> A unary true or false operator must take a single parameter of type T and must return type <keyword>bool</keyword>. </list_item></list></paragraph>
4   <paragraph>The signature of a unary operator consists of the operator token (+, -, !, ~, ++, --, true, or false) and the type of the single formal parameter. The return type is not part of a unary operator's signature, nor is the name of the formal parameter. </paragraph>
5   <paragraph>The true and false unary operators require pair-wise declaration. A compile-time error occurs if a class declares one of these operators without also declaring the other. The true and false operators are described further in <hyperlink>14.16</hyperlink>. </paragraph>
6   <paragraph>
7     <example>[Example: The following example shows an implementation and subsequent usage of operator++ for an integer vector class: <code_example><![CDATA[
8 public class IntVector  
9 {  
10    public int Length { ... }    // read-only property  
11    public int this[int index] { ... } // read-write indexer  
12    public IntVector(int vectorLength) { ... }  
13    public static IntVector operator++(IntVector iv) {  
14       IntVector temp = new IntVector(iv.Length);  
15       for (int i = 0; i < iv.Length; ++i)  
16       temp[i] = iv[i] + 1;  
17       return temp;  
18    }  
19 }  
20 class Test  
21 {  
22    static void Main() {  
23       IntVector iv1 = new IntVector(4);  // vector of 4x0  
24       IntVector iv2;  
25       
26       iv2 = iv1++;  // iv2 contains 4x0, iv1 contains 4x1  
27       iv2 = ++iv1;  // iv2 contains 4x2, iv1 contains 4x2  
28    }  
29 ]]></code_example></example>
30   </paragraph>
31   <paragraph>
32     <example>Note how the operator method returns the value produced by adding 1 to the operand, just like the postfix increment and decrement operators(<hyperlink>14.5.9</hyperlink>), and the prefix increment and decrement operators (<hyperlink>14.6.5</hyperlink>). </example>
33   </paragraph>
34   <paragraph>
35     <example>Unlike in C++, this method need not, and, in fact, must not, modify the value of its operand directly. end example]</example>
36   </paragraph>
37 </clause>