//Step1. 宣告Delegate物件 public delegate void MyDelegate(ref string output, string name); //Step 2.根據MyDelegate規格建立方法 private static void SayHello(ref string output, string name) { output += String.Format("Hello, {0}. ", name); } private static void GoodMorning(ref string output, string name) { output += String.Format("Good Morning, {0}. ", name); } //Step3. 宣告MyDelegate變數 MyDelegate handler; ///Step4. 將方法位置參考加入handler委派物件 handler += new MyDelegate(SayHello); handler += new MyDelegate( GoodMorning ); //Step5. 透過handler委派物件觸發方法執行 string data = ""; if (handler != null) { handler.Invoke(ref data, "JB"); }
結果 :
Hello, JB. Good Morning, JB.
沒有留言:
張貼留言