I. Description
In general, we define a delegate in two steps as follows:
Public Delegate void MyDelegate (string name); //define Delegates
Public MyDelegate mydelegate; Using delegates
But. NET also provides a well-defined delegate that we can use directly.
Second, the definition
System.Action no return value
Action:publicdelegatevoid Action (); action< t;: Publicdelegatevoid action< T > (t obj); action< T1, T2;: Publicdelegatevoid action< T1, T2 > (T1 arg1, T2 arg2);
Delegate void Action<t1,t2,t3,t4>t1 arg1, T2 arg2, T3 arg3, T4 arg4);
System.Func has a return value
func< TResult >publicdelegate TResult func< TResult > (); func< t,tresult >publicdelegate TResult func< T, TResult > (T arg); func< t1,t2,tresult >publicdelegate TResult func< T1, T2, TResult > (T1 arg1, T2 arg2);
*delegate TResult func<t1,t2,t3,t4,tresult>t1 arg1, T2 arg2, T3 arg3, T4 arg4);
Third, the use
Example 1:action
using Unityengine; using System.Collections; using System; Public class actiontest:monobehaviour { void Start () { action action = XXX; Action (); } void XXX () { Debug.Log ("+");} }
example 2:action<t>using unityengine; using System.Collections; using System; public class Actiontest:monobehaviour {void Start () {action<string> action = XXX; Action ( "Unity C #" ); } void XXX (string name) {Debug.Log (name); }}
Example 3:action<t1,t2>using unityengine; using System.Collections; using System; Public class actiontest:monobehaviour { void Start () { action<string,int> Action = xxx;< C10/>action ("Unity C #", +); } void XXX (string name,int score) { Debug.Log (string. Format ("{0} {1}", Name,score);} }
#region Action usage ///action<t> ///here T is the incoming type of the proxy function, no return value action<string []> Action = Span class= "KWRD" >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" , span class= "str" > "Jimmy" , "Selina" }; Action (str); Console.readkey (); #endregion
The example above is an array of string types passed in to find the entry containing the character s, and then output to the console.
Example 4:func<tresult >using unityengine; using System.Collections; using System; Public class functest:monobehaviour { void Start () { int > func= XXX; Debug.Log (func ()); } int XXX () { return ten; }}
Example 5:func<t,tresult>using unityengine; using System; Public Class functest:monobehaviour{ void Start () { func<string ,int> func= callstringlength; } int Callstringlength (string str) { return str. lenth;} }
func<string> Func=Delegate() { return' I am func<tresult> The result of the delegate return ";}
predicate can only accept one incoming parameter, the return value is type bool
#regionpredicatethe usage of///bool predicate<t> /// input a parameter of type T, the return value is type boolpredicate<string[]> predicate =Delegate(string[] x) {var result = from PinchXwhereP.contains ("S") Select P;if(result.) ToList (). Count > 0) {return true; }Else{return false; } };string[] _value = {"Charlies","Nancy","Alex","Jimmy","Selina"};if(predicate (_value)) {Console.WriteLine ("they contain."); }Else{Console.WriteLine ("They don ' t contain."); } console.readkey ();#endregion
The above code is also to determine whether the string array contains s of the item, and some words in the console print out they contain. If not, print out the They don ' t contain
//Definition Public void Object params Object where T:cuibase//Call cuimanager.instance.callui<cuimidmsg> ( (_ui, _arg) = _ui. ShowMsg (string) _arg[0]), string. Format (szmsg, format));
Part of the reference from: Wind Yu Chong Unity3d Tutorial Academy
This article is owned by the author and the blog Park, the source website: http://www.cnblogs.com/zhaoqingqing/. Welcome to reprint, but without the author's consent, reprint article must be in the article page obvious location to the author and the original link , otherwise reserve the right to pursue legal responsibility.
Delegate action and Func differences