C#中的三种委托方式:Func委托,Action委托,Predicate委托
C#中的三種委托方式:Func委托,Action委托,Predicate委托。
Func,Action,Predicate全面解析
首先來說明Func委托,通過MSDN我們可以了解到,Func委托有如下的5種類型:
??????????? (1) *delegate TResult Func<TResult>();
C#中的三種委托方式:Func委托,Action委托,Predicate委托以及這三種委托的常見使用場(chǎng)景。
Func,Action,Predicate全面解析
首先來說明Func委托,通過MSDN我們可以了解到,Func委托有如下的5種類型:
??????????? (1) *delegate TResult Func<TResult>();
??????????? (2)*delegate TResult Func<T1,TResult>(T1 arg1);
??????????? (3) *delegate TResult Func<T1,T2,TResult>(T1 arg1,T2 arg2);
??????????? (4)*delegate TResult Func<T1,T2,T3,TResult>(T1arg1, T2 arg2, T3 arg3);
??????????? (5)*delegate TResult Func<T1,T2,T3,T4,TResult>T1arg1, T2 arg2, T3 arg3, T4 arg4);
復(fù)制代碼
其中(1)只能委托無參但是有返回值的函數(shù),TResult就是其返回類型。
而(2)只能委托具有一個(gè)傳入?yún)?shù),有返回值的函數(shù),T1為一個(gè)傳入?yún)?shù),TResult為返回類型。
(3)只能委托具有二個(gè)傳入?yún)?shù),有返回值的函數(shù),T1和T2為兩個(gè)傳入?yún)?shù),TResult為返回類型,(4)和(5)以此類推。
那么如何來使用呢?下面給出一個(gè)簡(jiǎn)單的幾個(gè)例子:
?????????? #region Func委托
???????????
??????????? ///Func<TResult>的用法
??????????? ///這里TResult代表函數(shù)的返回值類型
??????????? ///只能代理返回值為TResult類型的無參函數(shù)
??????????? Func<string> func = delegate()
??????????? {
??????????????? return"我是Func<TResult>委托出來的結(jié)果";
??????????? };
??????????? Console.WriteLine(func());
??????????? Console.ReadKey();
??????????? ///Func<T,TResult>的用法
??????????? ///這里的T為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
??????????? ///只能代理參數(shù)為T類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string> funcOne = delegate(string s)
???????? ???{
??????????????? return s.ToUpper();
??????????? };
??????????? Console.WriteLine(funcOne("我是Func<T,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? ///Func<T1,T2,TResult>的用法
??????????? ///這里T1,T2為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
????????? ??///只能代理參數(shù)為T1,T2類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string, string> funcTwo = delegate(string value1, string value2)
??????????? {
??????????????? return value1 + "" + value2;
??????????? };
??????????? Console.WriteLine(funcTwo("我是", "Func<T1,T2,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? #endregion
復(fù)制代碼
上面代碼中,我用了匿名方法來代替函數(shù),其中delegate()代表無參函數(shù),delegate(string s)代表有一個(gè)傳入?yún)?shù)的函數(shù),以下的以此類推。
????? 然后需要說明的就是Action委托,這個(gè)委托也是非常常用的,尤其是在涉及到線程和界面交互的時(shí)候,配合著lamada表達(dá)式使用,非常方便的實(shí)現(xiàn)二者的交互。后面我會(huì)提到用法。
來看看Action委托的幾種表現(xiàn)形式:
??????????? (1) * delegatevoid Action(); 無參,無返回值
????????????? (2)* delegatevoid Action<T>(T1 arg1);
??????????? (3)* delegatevoid Action<T1,T2>(T1 arg1, T2 arg2);
??????????? (4)* delegatevoid Action<T1,T2,T3>T1 arg1, T2 arg2, T3 arg3);
??????????? (5)* delegatevoid Action<T1,T2,T3,T4>T1 arg1, T2 arg2, T3 arg3, T4 arg4);
復(fù)制代碼
從上面可以看出,總共有5中表現(xiàn)形式,其中(1)既沒有傳入?yún)?shù),也沒有返回值,那么它適合代理那些無參,無返回值的函數(shù);(2)有一個(gè)傳入?yún)?shù),無返回值,適合代理無參,有一個(gè)返回值的函數(shù),(3)(4)(5)以此類推。最都容納四個(gè)傳入?yún)?shù)。
那么如何使用呢?下面有一些簡(jiǎn)單的例子:
????????? #region Action的用法
??????????? ///Action<T>的用法
??????????? ///這里的T為代理函數(shù)的傳入類型,無返回值
??????????? Action<string[]> action = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
??????????????? foreach (string s in result.ToList())
??????????????? {
??????????????????? Console.WriteLine(s);
??????????????? }
??????????? };
??????????? string[] str={ "charlies","nancy","alex","jimmy","selina"};
??????????? action(str);
??????????? Console.ReadKey();
??????????? #endregion
復(fù)制代碼
上面的例子是通過傳入的String類型的數(shù)組,找出其中包含有字符s的項(xiàng),然后輸出到控制臺(tái)。
最后一個(gè)就是Predicate委托,這個(gè)的形式比較少一些,就是一個(gè)傳入?yún)?shù),返回值為bool類型,具體示例如下:
??????????? #region Predicate
????????? ///bool Predicate<T>的用法
??????????? ///輸入一個(gè)T類型的參數(shù),返回值為bool類型
??????????? Predicate<string[]> predicate = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
??????????????? if (result.ToList().Count > 0)
??????????????? {
??????????????????? returntrue;
??????????????? }
??????????????? else
??????????????? {
??????????????????? returnfalse;
??????????????? }
??????????? };
??????????? string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
??????????? if (predicate(_value))
??????????? {
??????????????? Console.WriteLine("They contain.");
??????????? }
??????????? else
??????????? {
??????????????? Console.WriteLine("They don't contain.");
??????????? }
?????????? ?Console.ReadKey();
??????????? #endregion
復(fù)制代碼
上面的代碼其實(shí)也是判斷String數(shù)組中有沒有包含s的項(xiàng),有的話就在控制臺(tái)打印出? They contain.沒有的話就打印出They don't contain.
總結(jié)一下這三個(gè)的特點(diǎn)就是:
Func可以接受0個(gè)至4個(gè)傳入?yún)?shù),必須具有返回值
Action可以接受0個(gè)至4個(gè)傳入?yún)?shù),無返回值
Predicate只能接受一個(gè)傳入?yún)?shù),返回值為bool類型
復(fù)制代碼
下面附上全部實(shí)現(xiàn)代碼:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateIntegrateConsoleApp
{
??? class Program
??? {
??????? staticvoid Main(string[] args)
??????? {
??????????? #region Func委托
???????????
??????????? ///Func<TResult>的用法
?? ?????????///這里TResult代表函數(shù)的返回值類型
??????????? ///只能代理返回值為TResult類型的無參函數(shù)
??????????? Func<string> func = delegate()
??????????? {
??????????????? return"我是Func<TResult>委托出來的結(jié)果";
??????????? };
??????????? Console.WriteLine(func());
??????????? Console.ReadKey();
??????????? ///Func<T,TResult>的用法
??????????? ///這里的T為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
??????????? ///只能代理參數(shù)為T類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string> funcOne = delegate(string s)
??????????? {
??????????????? return s.ToUpper();
??????????? };
??????????? Console.WriteLine(funcOne("我是Func<T,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? ///Func<T1,T2,TResult>的用法
??????????? ///這里T1,T2為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
??????????? ///只能代理參數(shù)為T1,T2類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string, string> funcTwo = delegate(string value1, string value2)
??????????? {
??????????????? return value1 + "" + value2;
??????????? };
??????????? Console.WriteLine(funcTwo("我是", "Func<T1,T2,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? /*************余下的類似上面的這種操作,最多可以接受四個(gè)傳入?yún)?shù)***************
???????????? *delegate TResultFunc<TResult>();?
???????????? *delegate TResultFunc<T1,TResult>(T1 arg1);
???????????? *delegate TResultFunc<T1,T2,TResult>(T1 arg1, T2 arg2);
????????? ???*delegate TResultFunc<T1,T2,T3,TResult>(T1 arg1, T2 arg2, T3 arg3);
???????????? *delegate TResultFunc<T1,T2,T3,T4,TResult>T1 arg1, T2 arg2, T3 arg3, T4 arg4);
???????????? */
??????????? #endregion
??????????? #region Action的用法
??????????? ///Action<T>的用法
??????????? ///這里的T為代理函數(shù)的傳入類型,無返回值
??????????? Action<string[]> action = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
???????? ???????foreach (string s in result.ToList())
??????????????? {
??????????????????? Console.WriteLine(s);
??????????????? }
??????????? };
??????????? string[] str={ "charlies","nancy","alex","jimmy","selina"};
??????????? action(str);
??????????? Console.ReadKey();
??????????? /***************余下的類似上面的這種操作,最多可以接受四個(gè)傳入?yún)?shù)**********
???????????? * delegate void Action(); 無參,無返回值
???????????? * delegate voidAction<T>(T1 arg1);
???????????? * delegate voidAction<T1,T2>(T1 arg1, T2 arg2);
???????????? * delegate voidAction<T1,T2,T3>T1 arg1, T2 arg2, T3 arg3);
???????????? * delegate voidAction<T1,T2,T3,T4>T1 arg1, T2 arg2, T3 arg3, T4 arg4);
???????????? */
??????????? #endregion
??????????? #region Predicate
??????????? ///bool Predicate<T>的用法
??? ????????///輸入一個(gè)T類型的參數(shù),返回值為bool類型
??????????? Predicate<string[]> predicate = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
???????????? ???if (result.ToList().Count > 0)
??????????????? {
??????????????????? returntrue;
??????????????? }
??????????????? else
??????????????? {
??????????????????? returnfalse;
??????????????? }
??????????? };
??????????? string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
??????????? if (predicate(_value))
??????????? {
??????????????? Console.WriteLine("They contain.");
??????????? }
??????????? else
??????????? {
??????????????? Console.WriteLine("They don't contain.");
??????????? }
?? ?????????Console.ReadKey();
??????????? #endregion
??????? }
??? }
}
復(fù)制代碼
在WinForm和WPF中,利用Func,Action,Predicate進(jìn)行線程UI交互
下面這部分主要講解如何在WinForm中利用這些委托進(jìn)行線程和界面的交互。
首先對(duì)于Func來說,由于其必須具有返回值,所以我們可以利用如下代碼來實(shí)現(xiàn)線程和界面的交互:
??????? #region利用Func實(shí)現(xiàn)線程和界面交互
??????? privatevoid AlternationUsingFunc(object text)
??????? {
??????????? //無參數(shù),但是返回值為bool類型
??????????? this.Invoke(new Func<bool>(delegate()
??????????? {
??????????????? button1.Text =text.ToString();
??????????????? returntrue; //返回值
??????????? }));
??????? }
??????? privatevoid AlternationUsingFuncThread()
??????? {
???? ???????WaitCallback waitCallBack = new WaitCallback(this.AlternationUsingFunc);
???????????ThreadPool.QueueUserWorkItem(waitCallBack, "Func的使用");
??????? }
??????? privatevoid button1_Click(object sender, EventArgs e)
??????? {
??????????? AlternationUsingFuncThread();
??????? }
??????? #endregion
復(fù)制代碼
其中
?this.Invoke(new Func<bool>(delegate()
??????????? {
??????????????? button1.Text =text.ToString();
??????????????? returntrue; //返回值
??????????? }));
復(fù)制代碼
這段代碼中利用了Func<TResult>這種類型,也就是沒有傳入?yún)?shù),但是有一個(gè)bool類型的返回值,然后將這個(gè)函數(shù)利用加入到線程池中,最后運(yùn)行,這里我們成功的設(shè)置了button1的text為“Func的使用”。
然后,對(duì)于Action來說,由于其可以無參,無返回值,那么它的交互方式最為簡(jiǎn)便,同時(shí)也是使用最多的,先看有參的調(diào)用方式:
??????? #region利用Action實(shí)現(xiàn)線程和界面交互
??????? privatevoid AlternationUsingAction(object text)
??????? {
?????????? ?//需要一個(gè)T類型的參數(shù),無返回值
??????????? this.Invoke(new Action<object>(delegate(object myText)
??????????? {
??????????????? myText = text;
??????????????? button2.Text =text.ToString();
??????????? }),text);
??????? }
??????? privatevoid AlternationUsingActionThread()
??????? {
??????????? WaitCallback waitCallBack = new WaitCallback(this.AlternationUsingAction);
???????????ThreadPool.QueueUserWorkItem(waitCallBack,"Action的使用");
??????? }
??????? privatevoid button2_Click(object sender, EventArgs e)
??????? {
???????????AlternationUsingActionThread();
??????? }
??????? #endregion
復(fù)制代碼
在上面的代碼示例中,我們使用了帶有一個(gè)傳入?yún)?shù)的Action委托,當(dāng)然了,匿名類型delegate(object myText)匿名代理了具有一個(gè)傳入?yún)?shù)的函數(shù)。
其實(shí)簡(jiǎn)單點(diǎn)來說,可以像如下方式使用:
this.Invoke((Action)(()=>
??????????? {
??????????????? button2.Text =text.ToString();
??????????? }));
復(fù)制代碼
這樣就顯得非常的方便。
最后一個(gè)當(dāng)然是Predicate委托,和上面類似,只是寫起來麻煩一些,它需要一個(gè)傳入?yún)?shù),并且返回一個(gè)bool類型:
??? ????#region利用Predicate實(shí)現(xiàn)線程和界面的交互
??????? privatevoid AlternationUsingPrecidate(object text)
??????? {
??????????? //需要一個(gè)T類型的參數(shù),返回bool類型
??????????? this.Invoke(new Predicate<object>(delegate(object myText)?
??????????? {
??????????????? myText = text;
??????????????? button3.Text =myText.ToString();
??????????????? returntrue;?? //返回值
??????????? }),text);
??????? }
??????? privatevoid AlternationUsingPrecidateThread()
??????? {
??????????? WaitCallback waitCallBack = new WaitCallback(this.AlternationUsingPrecidate);
???????????ThreadPool.QueueUserWorkItem(waitCallBack,"Predicate的使用");
??????? }
??????? privatevoid button3_Click(object sender, EventArgs e)
??????? {
???????????AlternationUsingPrecidateThread();
??????? }
??????? #endregion
復(fù)制代碼
具體的注釋我已經(jīng)寫在代碼中了,最后運(yùn)行,能成功的將button3的Text置為“Predicate的使用.”
?
??????????? (2)*delegate TResult Func<T1,TResult>(T1 arg1);
??????????? (3) *delegate TResult Func<T1,T2,TResult>(T1 arg1,T2 arg2);
??????????? (4)*delegate TResult Func<T1,T2,T3,TResult>(T1arg1, T2 arg2, T3 arg3);
??????????? (5)*delegate TResult Func<T1,T2,T3,T4,TResult>T1arg1, T2 arg2, T3 arg3, T4 arg4);
復(fù)制代碼
其中(1)只能委托無參但是有返回值的函數(shù),TResult就是其返回類型。
而(2)只能委托具有一個(gè)傳入?yún)?shù),有返回值的函數(shù),T1為一個(gè)傳入?yún)?shù),TResult為返回類型。
(3)只能委托具有二個(gè)傳入?yún)?shù),有返回值的函數(shù),T1和T2為兩個(gè)傳入?yún)?shù),TResult為返回類型,(4)和(5)以此類推。
那么如何來使用呢?下面給出一個(gè)簡(jiǎn)單的幾個(gè)例子:
?????????? #region Func委托
???????????
??????????? ///Func<TResult>的用法
??????????? ///這里TResult代表函數(shù)的返回值類型
??????????? ///只能代理返回值為TResult類型的無參函數(shù)
??????????? Func<string> func = delegate()
??????????? {
??????????????? return"我是Func<TResult>委托出來的結(jié)果";
??????????? };
??????????? Console.WriteLine(func());
??????????? Console.ReadKey();
??????????? ///Func<T,TResult>的用法
??????????? ///這里的T為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
??????????? ///只能代理參數(shù)為T類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string> funcOne = delegate(string s)
???????? ???{
??????????????? return s.ToUpper();
??????????? };
??????????? Console.WriteLine(funcOne("我是Func<T,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? ///Func<T1,T2,TResult>的用法
??????????? ///這里T1,T2為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
????????? ??///只能代理參數(shù)為T1,T2類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string, string> funcTwo = delegate(string value1, string value2)
??????????? {
??????????????? return value1 + "" + value2;
??????????? };
??????????? Console.WriteLine(funcTwo("我是", "Func<T1,T2,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? #endregion
復(fù)制代碼
上面代碼中,我用了匿名方法來代替函數(shù),其中delegate()代表無參函數(shù),delegate(string s)代表有一個(gè)傳入?yún)?shù)的函數(shù),以下的以此類推。
????? 然后需要說明的就是Action委托,這個(gè)委托也是非常常用的,尤其是在涉及到線程和界面交互的時(shí)候,配合著lamada表達(dá)式使用,非常方便的實(shí)現(xiàn)二者的交互。后面我會(huì)提到用法。
來看看Action委托的幾種表現(xiàn)形式:
??????????? (1) * delegatevoid Action(); 無參,無返回值
????????????? (2)* delegatevoid Action<T>(T1 arg1);
??????????? (3)* delegatevoid Action<T1,T2>(T1 arg1, T2 arg2);
??????????? (4)* delegatevoid Action<T1,T2,T3>T1 arg1, T2 arg2, T3 arg3);
??????????? (5)* delegatevoid Action<T1,T2,T3,T4>T1 arg1, T2 arg2, T3 arg3, T4 arg4);
復(fù)制代碼
從上面可以看出,總共有5中表現(xiàn)形式,其中(1)既沒有傳入?yún)?shù),也沒有返回值,那么它適合代理那些無參,無返回值的函數(shù);(2)有一個(gè)傳入?yún)?shù),無返回值,適合代理無參,有一個(gè)返回值的函數(shù),(3)(4)(5)以此類推。最都容納四個(gè)傳入?yún)?shù)。
那么如何使用呢?下面有一些簡(jiǎn)單的例子:
????????? #region Action的用法
??????????? ///Action<T>的用法
??????????? ///這里的T為代理函數(shù)的傳入類型,無返回值
??????????? Action<string[]> action = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
??????????????? foreach (string s in result.ToList())
??????????????? {
??????????????????? Console.WriteLine(s);
??????????????? }
??????????? };
??????????? string[] str={ "charlies","nancy","alex","jimmy","selina"};
??????????? action(str);
??????????? Console.ReadKey();
??????????? #endregion
復(fù)制代碼
上面的例子是通過傳入的String類型的數(shù)組,找出其中包含有字符s的項(xiàng),然后輸出到控制臺(tái)。
最后一個(gè)就是Predicate委托,這個(gè)的形式比較少一些,就是一個(gè)傳入?yún)?shù),返回值為bool類型,具體示例如下:
??????????? #region Predicate
????????? ///bool Predicate<T>的用法
??????????? ///輸入一個(gè)T類型的參數(shù),返回值為bool類型
??????????? Predicate<string[]> predicate = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
??????????????? if (result.ToList().Count > 0)
??????????????? {
??????????????????? returntrue;
??????????????? }
??????????????? else
??????????????? {
??????????????????? returnfalse;
??????????????? }
??????????? };
??????????? string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
??????????? if (predicate(_value))
??????????? {
??????????????? Console.WriteLine("They contain.");
??????????? }
??????????? else
??????????? {
??????????????? Console.WriteLine("They don't contain.");
??????????? }
?????????? ?Console.ReadKey();
??????????? #endregion
復(fù)制代碼
上面的代碼其實(shí)也是判斷String數(shù)組中有沒有包含s的項(xiàng),有的話就在控制臺(tái)打印出? They contain.沒有的話就打印出They don't contain.
總結(jié)一下這三個(gè)的特點(diǎn)就是:
Func可以接受0個(gè)至4個(gè)傳入?yún)?shù),必須具有返回值
Action可以接受0個(gè)至4個(gè)傳入?yún)?shù),無返回值
Predicate只能接受一個(gè)傳入?yún)?shù),返回值為bool類型
復(fù)制代碼
下面附上全部實(shí)現(xiàn)代碼:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateIntegrateConsoleApp
{
??? class Program
??? {
??????? staticvoid Main(string[] args)
??????? {
??????????? #region Func委托
???????????
??????????? ///Func<TResult>的用法
?? ?????????///這里TResult代表函數(shù)的返回值類型
??????????? ///只能代理返回值為TResult類型的無參函數(shù)
??????????? Func<string> func = delegate()
??????????? {
??????????????? return"我是Func<TResult>委托出來的結(jié)果";
??????????? };
??????????? Console.WriteLine(func());
??????????? Console.ReadKey();
??????????? ///Func<T,TResult>的用法
??????????? ///這里的T為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
??????????? ///只能代理參數(shù)為T類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string> funcOne = delegate(string s)
??????????? {
??????????????? return s.ToUpper();
??????????? };
??????????? Console.WriteLine(funcOne("我是Func<T,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? ///Func<T1,T2,TResult>的用法
??????????? ///這里T1,T2為代理的函數(shù)的傳入類型,TResult代表函數(shù)的返回值類型
??????????? ///只能代理參數(shù)為T1,T2類型,返回值為TResult類型的函數(shù)
??????????? Func<string, string, string> funcTwo = delegate(string value1, string value2)
??????????? {
??????????????? return value1 + "" + value2;
??????????? };
??????????? Console.WriteLine(funcTwo("我是", "Func<T1,T2,TResult>委托出來的結(jié)果"));
??????????? Console.ReadKey();
??????????? /*************余下的類似上面的這種操作,最多可以接受四個(gè)傳入?yún)?shù)***************
???????????? *delegate TResultFunc<TResult>();?
???????????? *delegate TResultFunc<T1,TResult>(T1 arg1);
???????????? *delegate TResultFunc<T1,T2,TResult>(T1 arg1, T2 arg2);
????????? ???*delegate TResultFunc<T1,T2,T3,TResult>(T1 arg1, T2 arg2, T3 arg3);
???????????? *delegate TResultFunc<T1,T2,T3,T4,TResult>T1 arg1, T2 arg2, T3 arg3, T4 arg4);
???????????? */
??????????? #endregion
??????????? #region Action的用法
??????????? ///Action<T>的用法
??????????? ///這里的T為代理函數(shù)的傳入類型,無返回值
??????????? Action<string[]> action = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
???????? ???????foreach (string s in result.ToList())
??????????????? {
??????????????????? Console.WriteLine(s);
??????????????? }
??????????? };
??????????? string[] str={ "charlies","nancy","alex","jimmy","selina"};
??????????? action(str);
??????????? Console.ReadKey();
??????????? /***************余下的類似上面的這種操作,最多可以接受四個(gè)傳入?yún)?shù)**********
???????????? * delegate void Action(); 無參,無返回值
???????????? * delegate voidAction<T>(T1 arg1);
???????????? * delegate voidAction<T1,T2>(T1 arg1, T2 arg2);
???????????? * delegate voidAction<T1,T2,T3>T1 arg1, T2 arg2, T3 arg3);
???????????? * delegate voidAction<T1,T2,T3,T4>T1 arg1, T2 arg2, T3 arg3, T4 arg4);
???????????? */
??????????? #endregion
??????????? #region Predicate
??????????? ///bool Predicate<T>的用法
??? ????????///輸入一個(gè)T類型的參數(shù),返回值為bool類型
??????????? Predicate<string[]> predicate = delegate(string[] x)
??????????? {
??????????????? var result = from p in x
???????????????????????????? where p.Contains("s")
???????????????????????????? select p;
???????????? ???if (result.ToList().Count > 0)
??????????????? {
??????????????????? returntrue;
??????????????? }
??????????????? else
??????????????? {
??????????????????? returnfalse;
??????????????? }
??????????? };
??????????? string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
??????????? if (predicate(_value))
??????????? {
??????????????? Console.WriteLine("They contain.");
??????????? }
??????????? else
??????????? {
??????????????? Console.WriteLine("They don't contain.");
??????????? }
?? ?????????Console.ReadKey();
??????????? #endregion
??????? }
??? }
}
復(fù)制代碼
在WinForm和WPF中,利用Func,Action,Predicate進(jìn)行線程UI交互
下面這部分主要講解如何在WinForm中利用這些委托進(jìn)行線程和界面的交互。
首先對(duì)于Func來說,由于其必須具有返回值,所以我們可以利用如下代碼來實(shí)現(xiàn)線程和界面的交互:
??????? #region利用Func實(shí)現(xiàn)線程和界面交互
??????? privatevoid AlternationUsingFunc(object text)
??????? {
??????????? //無參數(shù),但是返回值為bool類型
??????????? this.Invoke(new Func<bool>(delegate()
??????????? {
??????????????? button1.Text =text.ToString();
??????????????? returntrue; //返回值
??????????? }));
??????? }
??????? privatevoid AlternationUsingFuncThread()
??????? {
???? ???????WaitCallback waitCallBack = new WaitCallback(this.AlternationUsingFunc);
???????????ThreadPool.QueueUserWorkItem(waitCallBack, "Func的使用");
??????? }
??????? privatevoid button1_Click(object sender, EventArgs e)
??????? {
??????????? AlternationUsingFuncThread();
??????? }
??????? #endregion
復(fù)制代碼
其中
?this.Invoke(new Func<bool>(delegate()
??????????? {
??????????????? button1.Text =text.ToString();
??????????????? returntrue; //返回值
??????????? }));
復(fù)制代碼
這段代碼中利用了Func<TResult>這種類型,也就是沒有傳入?yún)?shù),但是有一個(gè)bool類型的返回值,然后將這個(gè)函數(shù)利用加入到線程池中,最后運(yùn)行,這里我們成功的設(shè)置了button1的text為“Func的使用”。
然后,對(duì)于Action來說,由于其可以無參,無返回值,那么它的交互方式最為簡(jiǎn)便,同時(shí)也是使用最多的,先看有參的調(diào)用方式:
??????? #region利用Action實(shí)現(xiàn)線程和界面交互
??????? privatevoid AlternationUsingAction(object text)
??????? {
?????????? ?//需要一個(gè)T類型的參數(shù),無返回值
??????????? this.Invoke(new Action<object>(delegate(object myText)
??????????? {
??????????????? myText = text;
??????????????? button2.Text =text.ToString();
??????????? }),text);
??????? }
??????? privatevoid AlternationUsingActionThread()
??????? {
??????????? WaitCallback waitCallBack = new WaitCallback(this.AlternationUsingAction);
???????????ThreadPool.QueueUserWorkItem(waitCallBack,"Action的使用");
??????? }
??????? privatevoid button2_Click(object sender, EventArgs e)
??????? {
???????????AlternationUsingActionThread();
??????? }
??????? #endregion
復(fù)制代碼
在上面的代碼示例中,我們使用了帶有一個(gè)傳入?yún)?shù)的Action委托,當(dāng)然了,匿名類型delegate(object myText)匿名代理了具有一個(gè)傳入?yún)?shù)的函數(shù)。
其實(shí)簡(jiǎn)單點(diǎn)來說,可以像如下方式使用:
this.Invoke((Action)(()=>
??????????? {
??????????????? button2.Text =text.ToString();
??????????? }));
復(fù)制代碼
這樣就顯得非常的方便。
最后一個(gè)當(dāng)然是Predicate委托,和上面類似,只是寫起來麻煩一些,它需要一個(gè)傳入?yún)?shù),并且返回一個(gè)bool類型:
??? ????#region利用Predicate實(shí)現(xiàn)線程和界面的交互
??????? privatevoid AlternationUsingPrecidate(object text)
??????? {
??????????? //需要一個(gè)T類型的參數(shù),返回bool類型
??????????? this.Invoke(new Predicate<object>(delegate(object myText)?
??????????? {
??????????????? myText = text;
??????????????? button3.Text =myText.ToString();
??????????????? returntrue;?? //返回值
??????????? }),text);
??????? }
??????? privatevoid AlternationUsingPrecidateThread()
??????? {
??????????? WaitCallback waitCallBack = new WaitCallback(this.AlternationUsingPrecidate);
???????????ThreadPool.QueueUserWorkItem(waitCallBack,"Predicate的使用");
??????? }
??????? privatevoid button3_Click(object sender, EventArgs e)
??????? {
???????????AlternationUsingPrecidateThread();
??????? }
??????? #endregion
復(fù)制代碼
具體的注釋我已經(jīng)寫在代碼中了,最后運(yùn)行,能成功的將button3的Text置為“Predicate的使用.”
?
轉(zhuǎn)載于:https://www.cnblogs.com/wangyhua/archive/2012/09/29/4050624.html
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的C#中的三种委托方式:Func委托,Action委托,Predicate委托的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页上有错误(类不能支持 Automat
- 下一篇: DIV+CSS布局图片加阴影效果方法