Action、Func


需求:2018年Apple的MacBookPro新電腦已經出來了,這年的電腦增加了許多亮點,比如RAM可配置成32G,小A看到后,欣喜若狂,於是,亦然決然第要要買一台。

1、用委托的方式購買。

由於Apple公司是在美國,為了買個MacBookPro難道還要跑到美國嗎(這樣加上路費成本會高很多,如果專門跑到美國購買,太不划算了)?不用專門跑到美國買,現在可以通過蘋果指定實體店、電商平台購買和官方旗艦店購買。於是乎,小A決定在Apple的官方旗艦店上訂購一台。

聲明委托:

//聲明一個委托購買MacBookPro的委托。
private delegate void BuyMacBookPro();

聲明委托執行的方法:

//電商平台(委托要執行的方法)。
private static void BuyMacBookProCallBack() { Console.WriteLine("我是Apple官方旗艦店,恭喜你成功訂購了一台MacBookPro!"); }

將委托與方法建立聯系:

static void Main(string[] args) { //小A和Apple官方旗艦店建立聯系(給委托指定方法)。
    BuyMacBookPro buyMacBookPro = new BuyMacBookPro(BuyMacBookProCallBack); //小A給Apple官方旗艦店打錢,Apple官方旗艦店給小A發電腦(觸發)。
 buyMacBookPro(); }

2、用Action的方式購買。

9月份即將到來,一年一度的IPhone又該與大家見面了,小A是個數碼控,於是辛辛苦苦吃土3個月決定買一個IPhone。經過購買MacBookPro的經驗來講,每次購買都要聲明一個委托,有沒有什么好一點的辦法買IPhone時不用定義委托?於是乎,可以使用Action,只需要傳入你要購買的手機型號即可。

聲明Action要執行的方法:

private static void BuyIPhoneCallBack(string _name) { Console.WriteLine("我是Apple官方旗艦店,恭喜你成功訂購了一台{0}", _name); }

聲明Action並觸發:

static void Main(string[] args) { //實例化Action,為其指定要執行的函數。
    Action<string> action = new Action<string>(BuyIPhoneCallBack); //觸發。
    action("IPhone9"); }

3、Func。

Func:封裝一個不具有參數(也可有參數)但卻返回TResult參數指定的類型的值的方法。

3.1)、沒有參數的Func。

static void Main(string[] args) { //封裝一個不具有參數(也可有參數)但卻返回TResult參數指定的類型的值的方法。 //string:指返回值類型。
    Func<string> func = new Func<string>(FuncCallBack); string result = func(); Console.WriteLine(result); Console.ReadLine(); } private static string FuncCallBack() { return "OK"; }

結果為 "OK"。

3.2)、有參數的Func。

static void Main(string[] args) { //有5個int類型的參數,返回值是int類型。
    Func<int, int, int, int, int, int> func = new Func<int, int, int, int, int, int>(SumCallBack); //觸發,填入參數。
    int result = func(1, 2, 3, 4, 5); Console.WriteLine(result); Console.ReadLine(); } private static int SumCallBack(int arg1, int arg2, int arg3, int arg4, int arg5) { return arg1 + arg2 + arg3 + arg4 + arg5; }

結果為15

3.3)、用Func傳遞值。 

static void Main(string[] args) { //還可以簡寫成: //Func<string> func = delegate { return "OK"; };/ Func<string> func = () => { return "OK"; };
    Func<string> func = new Func<string>(delegate { return "我是通過Func的方式傳遞值"; }); DisplayFuncValue(func); Console.ReadLine(); } private static void DisplayFuncValue(Func<string> _func) { string result = _func(); Console.WriteLine(result); }

結果為:

4、什么時候用Action,什么時候用Func呢?

Action用於沒有返回值的方法(參數可以根據自己情況進行傳遞)。

Func恰恰相反用於有返回值的方法(同樣參數根據自己情況情況)。

記住無返回就用action,有返回就用Func。

5、Task。

task中文意思是工作、任務的意思。

task:表示一個可以返回值的異步操作。

5.1)、創建Task。

創建Task有兩種方式:分別是實例化Task對象和通過Factory.StartNew()方法。

 a)、實例化對象的方法創建Task。

static void Main(string[] args) { //聲明Action類型的委托。
    Action action = new Action(ActionCallBack); //實例化Task對象,傳入一個委托。
    Task task = new Task(action); //啟動 System.Threading.Tasks.Task,並將它安排到當前的 System.Threading.Tasks.TaskScheduler中。
 task.Start(); Console.ReadLine(); } private static void ActionCallBack() { Console.WriteLine("我是Action委托執行的方法。"); }

b)、通過Factory.StartNew()。

static void Main(string[] args) { //聲明Action類型的委托。
    Action action = new Action(ActionCallBack); Task.Factory.StartNew(action); Console.ReadLine(); } private static void ActionCallBack() { Console.WriteLine("我是Action委托執行的方法。"); }

 5.2)、Result屬性。

為其傳入泛型參數,指定返回類型,通過Result屬性可獲取返回值。

static void Main(string[] args) { //指定返回類型為int類型。
    Task<int> task = new Task<int>(TestMethod); task.Start(); //當走到task.Result時,進入TestMethod方法,Result屬性適合得到有返回值的函數。
    int result = task.Result; Console.WriteLine(result); Console.ReadLine(); } private static int TestMethod() { return 12345; }

返回結果為:12345

 5.3)、為Task傳入參數Func類型的參數。

static void Main(string[] args) { //也可寫成:Func<string> func = () => { return "OK"; };
    Func<string> func = new Func<string>(TestCallBack); //實例化Task對象,為其傳入Func類型的參數。
    Task<string> task = new Task<string>(func); task.Start(); Console.WriteLine(task.Result); Console.ReadLine(); } private static string TestCallBack() { return "OK"; }

 6、線程同步(lock(this))。

class Program { static void Main(string[] args) { SaleBook sb = new SaleBook(); Thread th1 = new Thread(new ParameterizedThreadStart(sb.MaiShu)); Thread th2 = new Thread(new ParameterizedThreadStart(sb.MaiShu)); th1.Start("線程1"); th2.Start("線程2"); Console.ReadKey(); } } public class SaleBook { int bookCount = 1; public void MaiShu(object _threadName) { Console.WriteLine(string.Format("名叫{0}的線程調用了此方法", _threadName.ToString())); lock (this) { int tmp = bookCount; if (tmp > 0) //判斷是否有書,如果有就可以賣
 { Thread.Sleep(1000); bookCount = bookCount - 1; Console.WriteLine("剛售出一本書,還剩{0}本", bookCount); } else { Console.WriteLine("不好意思,書已賣完,歡迎下次光臨!"); } } } }

參考博客鏈接:https://www.cnblogs.com/LipeiNet/p/4694225.html

End!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM