delegate、Action、Func的用法

委託的特色安全

  1. 委託相似於 C++ 函數指針,但它們是類型安全的。
  2. 委託容許將方法做爲參數進行傳遞。
  3. 委託可用於定義回調方法。
  4. 委託能夠連接在一塊兒。

delegate的用法函數

delegate void BookDelegate(string a,string b);
public MainWindow()
{
    InitializeComponent();
    BookDelegate method = new BookDelegate(Book);
    method("Hello", "World");
}
public void Book(string a, string b)
{

}

 Action的用法spa

Action<string, string> BookAction = new Action<string, string>(Book);
public MainWindow()
{
    InitializeComponent();
    BookAction("Hello", "World");
}
public static void Book(string a, string b)
{

}

 Func的用法指針

Func<string, string, string> FuncBook = new Func<string, string, string>(Book);
public MainWindow()
{
    InitializeComponent();
    Book("Hello", "World");
}
public static string Book(string a, string b)
{
    return String.Format("{0}{1}", a, b);
}

總結orm

  1. Delegate至少0個參數,至多32個參數,能夠無返回值,也能夠指定返回值類型
  2. Func能夠接受0個至16個傳入參數,必須具備返回值
  3. Action能夠接受0個至16個傳入參數,無返回值
相關文章
相關標籤/搜索