[TestMethod] // ----------------------------------------------------------------------- public void TestMethod2() { Timestwo mul_two = delegate(int x) { return(2 * x); }; // delegate 2.實作(注意函式名稱在原本type的位置) 3. 指定委派 Multiply mul = delegate(int x, int y) { return(x * y); }; HLog.print("TestMethod2 mul_two = " + mul_two(5).ToString()); // delegate 4. 使用 HLog.print("TestMethod2 mul = " + mul(5, 6).ToString()); }
private void Test1_Click(object sender, RoutedEventArgs e) { // 1. 委派可以將方法當成參數來進行傳遞 // 委派語法:[public|private|protected] Delegate[void | 回傳資料型態] 委派名稱([參數1, 參數2,…]); Timestwo timestwo = delegate(int x) { return(2 * x); }; Multiply multiply = delegate(int x, int y) { return(x * y); }; txt_timestwo.Text = timestwo(5).ToString(); txt_multiply.Text = multiply(5, 6).ToString(); }