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 17 Classesprevious at this levelnext at this level 17.9 Operatorsprevious at this levelnext at this level 17.9.1 Unary operators Paragraph 11 The following rules apply to unary operator declarations, where T denotes the class or struct type that contains the operator declaration: Paragraph 21 The signature of a unary operator consists of the operator token (+, -, !, ~, ++, --, true, or false) and the type of the single formal parameter. 2 The return type is not part of a unary operator's signature, nor is the name of the formal parameter. Paragraph 31 The true and false unary operators require pair-wise declaration. 2 A compile-time error occurs if a class declares one of these operators without also declaring the other. 3 The true and false operators are described further in §14.16. [Example: The following example shows an implementation and subsequent usage of operator++ for an integer vector class:
public class IntVector  
{  
   public int Length { ... }    // read-only property  
   public int this[int index] { ... } // read-write indexer  
   public IntVector(int vectorLength) { ... }  
   public static IntVector operator++(IntVector iv) {  
      IntVector temp = new IntVector(iv.Length);  
      for (int i = 0; i < iv.Length; ++i)  
      temp[i] = iv[i] + 1;  
      return temp;  
   }  
}  
class Test  
{  
   static void Main() {  
      IntVector iv1 = new IntVector(4);  // vector of 4x0  
      IntVector iv2;  
      
      iv2 = iv1++;  // iv2 contains 4x0, iv1 contains 4x1  
      iv2 = ++iv1;  // iv2 contains 4x2, iv1 contains 4x2  
   }  
Note how the operator method returns the value produced by adding 1 to the operand, just like the postfix increment and decrement operators(§14.5.9), and the prefix increment and decrement operators (§14.6.5). Unlike in C++, this method need not, and, in fact, must not, modify the value of its operand directly. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS