Action和Func最大差異: Action 無回傳值, Func有回傳值

   1: public static Action A1 = () =>
   2:       {
   3:           MessageBox.Show("call Action A");
   4:       };
   5:       // 輸入參數: 1個
   6:       public static Action<string> A2 = (x) =>
   7:       {
   8:           MessageBox.Show(x);
   9:       };
  10:       // 輸入參數: 2個
  11:       public static Action<string, string> A3 = (x1, x2) =>
  12:       {
  13:          MessageBox.Show( x1 + x2);
  14:       };
  15:       // 輸入參數: 3個
  16:       public static Action<int, int, int> A4 = (x1, x2, x3) =>
  17:       {
  18:           bool check1 = (x1 + x2) > x3;
  19:           bool check2 = (x2 + x3) > x1;
  20:           bool check3 = (x1 + x3) > x2;
  21:           if (check1 & check2 & check3)
  22:           {
  23:               MessageBox.Show("合法三角形");
  24:           }
  25:           else
  26:           {
  27:               MessageBox.Show("不合法三角形");
  28:           }
  29:        };
  30:        // 輸入參數: 4個
  31:        public static Action<int, int, int, string> A5 = (x1, x2, x3, str) =>
  32:        {
  33:           if( !string.IsNullOrEmpty(str))
  34:           MessageBox.Show(str);
  35:           bool check1 = (x1 + x2) > x3;
  36:           bool check2 = (x2 + x3) > x1;
  37:           bool check3 = (x1 + x3) > x2;
  38:           if (check1 & check2 & check3)
  39:           {
  40:               MessageBox.Show("合法三角形");
  41:           }
  42:           else
  43:           {
  44:               MessageBox.Show("不合法三角形");
  45:           }
  46:        };

Func < arg1, arg2, …>最後一參數都是留給輸出, 其他則為輸入

   1: // 一個輸入(1) 一個輸出(2)
   2:        public static Func<string, string> F1 = (x) =>
   3:        {
   4:            return string.Format("Hello {0}!", x);
   5:        };
   6:        // 兩個輸入(1,2)  一個輸出(3)
   7:        public static Func<int, int, string> F2 = (x1, x2) =>
   8:        {
   9:            return string.Format("{0} + {1} = {2}", x1, x2, (x1 + x2));
  10:        };
  11:        // 三個輸入(1,2,3)  一個輸出(4)
  12:        public static Func<int, int, int, string> F3 = (x1, x2, x3) =>
  13:        {
  14:            return string.Format("{0}*({1} + {2}) = {3}", x1, x2, x3, x1*(x2 + x3));
  15:        };
  16:        // 四個輸入(1,2,3,4)  一個輸出(5)
  17:        public static Func<double, double, double, double, string> F4 = (w1, x1, w2, x2) =>
  18:        {
  19:            return string.Format("{0}*{1} + {2}*{3} = {4}", w1, x1, w2, x2,  w1*x1+w2*x2);
  20:        };

Func為一種泛型,定義如下:

Func <T, TResult> Delegate

Encapsulates a method that has one parameter and returns a value of the type specified by the TResult

image

   1: private void button1_Click(object sender, EventArgs e)
   2:        {
   3:             ActionSample.A1();
   4:             ActionSample.A2(DateTime.Now.ToLongTimeString());
   5:             ActionSample.A3("Hello", " world");
   6:             ActionSample.A4(5, 8, 4);
   7:             ActionSample.A5(5, 8, 4, "三角形檢查");
   8:        }

 

   1: private void button2_Click(object sender, EventArgs e)
   2:      {
   3:           string str;
   4:           str = FuncSample.F1("Peter"); MessageBox.Show(str);
   5:           str = FuncSample.F2(5, 10); MessageBox.Show(str);
   6:           str = FuncSample.F3(2, 1, 3); MessageBox.Show(str);
   7:           str = FuncSample.F4(0.5, 2, 0.5, 4); MessageBox.Show(str);
   8:      }

 

題外話: 撰寫class優先順序:   靜態函式 > Singleton Pattern:   呼叫時語法相對簡潔

image

 

參考資料:

  1. [C#] 泛型委派 Action<T> 與 Func<T>

  2. C# Lambda運算式

  3. C# Delegates with Anonymous Methods(匿名方法)and Named Methods(具名方法)


  4. Action 委派
  5. Action(T) 委派
  6. Action(T1, T2) 委派
  7. Action(T1, T2, T3) 委派
  8. Action(T1, T2, T3, T4) 委派
  9. Action(T1, T2, T3, T4, T5) 委派
  10. Action(T1, T2, T3, T4, T5, T6) 委派
  11. Action(T1, T2, T3, T4, T5, T6, T7) 委派
  12. Action(T1, T2, T3, T4, T5, T6, T7, T8) 委派
  13. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9) 委派
  14. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) 委派
  15. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) 委派
  16. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) 委派
  17. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) 委派
  18. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) 委派
  19. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) 委派
  20. Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) 委派

  21. Func(TResult) 委派
  22. Func(T, TResult) 委派
  23. Func(T1, T2, TResult) 委派
  24. Func(T1, T2, T3, TResult) 委派
  25. Func(T1, T2, T3, T4, TResult) 委派
  26. Func(T1, T2, T3, T4, T5, TResult) 委派
  27. Func(T1, T2, T3, T4, T5, T6, TResult) 委派
  28. Func(T1, T2, T3, T4, T5, T6, T7, TResult) 委派
  29. Func(T1, T2, T3, T4, T5, T6, T7, T8, TResult) 委派
  30. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult) 委派
  31. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult) 委派
  32. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult) 委派
  33. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult) 委派
  34. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult) 委派
  35. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult) 委派
  36. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult) 委派
  37. Func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult) 委派
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()