您好,登錄后才能下訂單哦!
這篇文章主要介紹“c#中Delegate,Action,F(xiàn)unc和Predicate怎么使用”,在日常操作中,相信很多人在c#中Delegate,Action,F(xiàn)unc和Predicate怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”c#中Delegate,Action,F(xiàn)unc和Predicate怎么使用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
1.Delegate,Action(常用)
#region 模塊信息 // ********************************************************************** // Copyright (C) 2018 Blazors // Please contact me if you have any questions // File Name: GameDefine // Author: romantic123fly // We // ********************************************************************** #endregion using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public delegate void MyDelegate(string name);//定義委托 public delegate void Action(); public delegate void Action<T>(T obj); public delegate void Action<T1, T2>(T1 arg1, T2 arg2); public class ActionTest : MonoBehaviour { public MyDelegate myDelegate; //使用委托 Action action; Action<string> action1; Action<string, int> action2; Action<string[]> action3; void Start() { myDelegate += DelegateFun; myDelegate("myDelegate"); action += test1;//無參數(shù) action(); action1 += test2;//一個(gè)參數(shù) action1("Test2"); action2 += test3;//兩個(gè)參數(shù) action2("Test3", 99); action3 += test4;//集合參數(shù) action3(new string[] { "charlies", "nancy", "alex", "jimmy", "selina" }); } private void DelegateFun(string name) { Debug.Log(name); } void test1() { Debug.Log("test1"); } void test2(string str) { Debug.Log(str); } void test3(string str,int num) { Debug.Log(string.Format("{0} {1}", str, num)); } void test4(string[] x) { var result = from o in x where o.Contains("s")select o; foreach (string s in result.ToList()) { Debug.Log(s); } } }
Func可以傳入多個(gè)參數(shù),默認(rèn)最后一個(gè)為返回值 Predicate只能接受一個(gè)傳入?yún)?shù),返回值為bool類型
#region 模塊信息 // ********************************************************************** // Copyright (C) 2018 Blazors // Please contact me if you have any questions // File Name: GameDefine // Author: romantic123fly // WeChat // ********************************************************************** #endregion using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; //有返回值的委托 public delegate TResult Func<TResult>(); public delegate TResult Func<T, TResult>(T arg); public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2); public class FuncTest : MonoBehaviour { Func<int> func; Func<string, int> func1; Predicate<string[]> predicate; void Start() { func = XXX; Debug.Log(func()); func1 = CallStringLength; Debug.Log(func1("sadasdads")); ///bool Predicate<T>的用法 ///輸入一個(gè)T類型的參數(shù),返回值為bool類型 predicate = func2; string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" }; Debug.Log(predicate(_value)); } private bool func2(string[] obj) { var result = from p in obj where p.Contains("s") select p; if (result.ToList().Count > 0) { return true; } else { return false; } } int XXX() { return 10; } int CallStringLength(string str) { return str.Length; } }
到此,關(guān)于“c#中Delegate,Action,F(xiàn)unc和Predicate怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。