1: using System;
2:
3: namespace Example
4: {
5: public class Test4
6: {
7: public string strMyValue;
8: public Test4()
9: {
10: Console.WriteLine("Test4 () Constructor");
11: strMyValue = "Doh!";
12: }
13: public Test4(string inMyValue)
14: {
15: Console.WriteLine(" Test4 ({0}) Constructor", inMyValue);
16: strMyValue = inMyValue;
17: }
18: ~Test4()
19: {
20: Console.WriteLine(" Test4 Finalize [{0}]", strMyValue);
21: }
22: //here we overide the implicit string conversion
23: public static implicit operator string(Test4 t)
24: {
25: return "Test4:("+t.strMyValue+")";
26: }
27: //and here we add a conversion to int
28: public static implicit operator int(Test4 t)
29: {
30: return 42;
31: }
32:
33: }
34:
35: public class ImplicitExample
36: {
37: static public object objHolder; // defaults to null
38:
39: static void Main(string[] args)
40: {
41: object one;
42:
43: Console.WriteLine("BEGIN:");
44: Console.WriteLine(" Create a new Test4");
45: one = new Test4("blah");
46: Console.WriteLine(" one ->"+one);
47: Console.WriteLine(" (Test4)one->"+(Test4)one);
48: Console.WriteLine(" (int)(Test4)one->"+(int)((Test4)one));
49: Console.WriteLine(" Destruct our Test4");
50: one = null;
51: Console.WriteLine("END");
52: }
53: }
54: }
55: