1 // Namespace Declaration 2 using System; 3 4 // helper class 5 class OutputClass 6 { 7 string myString; 8 9 // Constructor 10 public OutputClass(string inputString) 11 { 12 myString = inputString; 13 } 14 15 //this指向特定对象,它产生了对自己带参构造函数的调用 16 public OutputClass() : this("default string") 17 { 18 } 19 20 // Instance Method 21 public void printString() 22 { 23 Console.WriteLine("{0}", myString); 24 } 25 26 // Destructor 27 ~OutputClass() 28 { 29 // Some resource cleanup routines 30 } 31 } 32 33 // Program start class 34 class ExampleClass 35 { 36 // Main begins program execution. 37 public static void Main() 38 { 39 // Instance of OutputClass 40 OutputClass outCl = new OutputClass(); 41 42 OutputClass outC2 = new OutputClass("This is printed by the output class."); 43 44 // Call Output class' method 45 outCl.printString(); 46 47 outC2.printString(); 48 } 49 }