[llvm] Fix JIT support.
[mono.git] / mcs / docs / ecma334 / 8.7.6.xml
1 <?xml version="1.0"?>
2 <clause number="8.7.6" title="Operators" informative="true">
3   <paragraph>An operator is a member that defines the meaning of an expression operator that can be applied to instances of the class. There are three kinds of operators that can be defined: unary operators, binary operators, and conversion operators. </paragraph>
4   <paragraph>The following example defines a Digit type that represents <keyword>decimal</keyword> digits-integral values between 0 and 9. <code_example><![CDATA[
5 using System;  
6 public struct Digit  
7 {  
8    byte value;  
9    public Digit(byte value) {  
10       if (value < 0 || value > 9) throw new ArgumentException();  
11       this.value = value;  
12    }  
13    public Digit(int value): this((byte) value) {}  
14    public static implicit operator byte(Digit d) {  
15       return d.value;  
16    }  
17    public static explicit operator Digit(byte b) {  
18       return new Digit(b);  
19    }  
20    public static Digit operator+(Digit a, Digit b) {  
21       return new Digit(a.value + b.value);  
22    }  
23    public static Digit operator-(Digit a, Digit b) {  
24       return new Digit(a.value - b.value);  
25    }  
26    public static bool operator==(Digit a, Digit b) {  
27       return a.value == b.value;  
28    }  
29    public static bool operator!=(Digit a, Digit b) {  
30       return a.value != b.value;  
31    }  
32    public override bool Equals(object value) {  
33       if (value == null) return false;  
34       if (GetType() == value.GetType()) return this == (Digit)value;  
35    return false;  }  
36    public override int GetHashCode() {  
37       return value.GetHashCode();  
38    }  
39    public override string ToString() {  
40       return value.ToString();  
41    }  
42 }  
43 class Test  
44 {  
45    static void Main() {  
46       Digit a = (Digit) 5;  
47       Digit b = (Digit) 3;  
48       Digit plus = a + b;  
49       Digit minus = a - b;  
50       bool equals = (a == b);  
51       Console.WriteLine("{0} + {1} = {2}", a, b, plus);  
52       Console.WriteLine("{0} - {1} = {2}", a, b, minus);  
53       Console.WriteLine("{0} == {1} = {2}", a, b, equals);  
54    }  
55 }  
56 ]]></code_example></paragraph>
57   <paragraph>The Digit type defines the following operators: <list><list_item> An implicit conversion operator from Digit to <keyword>byte</keyword>. </list_item><list_item> An explicit conversion operator from <keyword>byte</keyword> to Digit. </list_item><list_item> An addition operator that adds two Digit values and returns a Digit value. </list_item><list_item> A subtraction operator that subtracts one Digit value from another, and returns a Digit value. </list_item><list_item> The equality (==) and inequality (!=) operators, which compare two Digit values. </list_item></list></paragraph>
58 </clause>