5 public class MyBoolean
{
6 public static implicit operator bool (MyBoolean x
)
12 public class MyTrueFalse
{
13 public static bool operator true (MyTrueFalse i
)
18 public static bool operator false (MyTrueFalse i
)
28 public static int Main ()
32 k
= new Blah (2) + new Blah (3);
49 k
= new Blah (22) - new Blah (21);
54 Console
.WriteLine ("! returned true");
64 Console
.WriteLine ("k is definitely true");
68 double f
= (double) k
;
73 int i
= new Blah (5) * new Blah (10);
91 MyBoolean myb
= new MyBoolean ();
97 // Tests the conditional operator invoking operator true
98 MyTrueFalse mf
= new MyTrueFalse ();
103 Console
.WriteLine ("Test passed");
112 public static Blah
operator + (Blah i
, Blah j
)
114 Blah b
= new Blah (i
.i
+ j
.i
);
115 Console
.WriteLine ("Overload binary + operator");
119 public static Blah
operator + (Blah i
)
121 Console
.WriteLine ("Overload unary + operator");
122 return new Blah (i
.i
);
125 public static Blah
operator - (Blah i
)
127 Console
.WriteLine ("Overloaded unary - operator");
128 return new Blah (- i
.i
);
131 public static Blah
operator - (Blah i
, Blah j
)
133 Blah b
= new Blah (i
.i
- j
.i
);
134 Console
.WriteLine ("Overloaded binary - operator");
138 public static int operator * (Blah i
, Blah j
)
140 Console
.WriteLine ("Overloaded binary * operator");
144 public static int operator / (Blah i
, Blah j
)
146 Console
.WriteLine ("Overloaded binary / operator");
150 public static int operator % (Blah i
, Blah j
)
152 Console
.WriteLine ("Overloaded binary % operator");
156 public static Blah
operator ~
(Blah i
)
158 Console
.WriteLine ("Overloaded ~ operator");
159 return new Blah (~i
.i
);
162 public static bool operator ! (Blah i
)
164 Console
.WriteLine ("Overloaded ! operator");
168 public static Blah
operator ++ (Blah i
)
170 Blah b
= new Blah (i
.i
+ 1);
171 Console
.WriteLine ("Incrementing i");
175 public static Blah
operator -- (Blah i
)
177 Blah b
= new Blah (i
.i
- 1);
178 Console
.WriteLine ("Decrementing i");
182 public static bool operator true (Blah i
)
184 Console
.WriteLine ("Overloaded true operator");
188 public static bool operator false (Blah i
)
190 Console
.WriteLine ("Overloaded false operator");
194 public static implicit operator int (Blah i
)
196 Console
.WriteLine ("Converting implicitly from Blah->int");
200 public static explicit operator double (Blah i
)
202 Console
.WriteLine ("Converting explicitly from Blah->double");