Difference between delegate Action and Func: Delegate actionfunc

Source: Internet
Author: User

Difference between delegate Action and Func: Delegate actionfunc
I. Description

Generally, we define the delegate as follows:

Public delegate void MyDelegate (string name); // defines the delegate
Public MyDelegate myDelegate; // use the delegate

However,. Net also provides defined delegation, which can be used directly.

Ii. Definition
System. Action No Return Value
Action:public delegate void Action ();Action< T >:public delegate void Action< T > (T obj);Action< T1, T2 >:public delegate void 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 returned values.
Func< TResult >public delegate TResult Func< TResult > ();Func< T,TResult >public delegate TResult Func< T, TResult > (T arg);Func< T1,T2,TResult >public delegate 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);
Iii. 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("100");    }}
 
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; action ("unity C #", 100 );} void XXX (string name, int score) {Debug. log (string. format ("{0} {1}", name, score );}}
 
# Region Action usage // Action <T> usage // here T is the input type of the proxy function, no return value 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
In the preceding example, an array of the String type is input to find the item containing the character s and output it to the console.
 
Example 4: Func <TResult> using UnityEngine; using System. collections; using System; public class FuncTest: MonoBehaviour {void Start () {Func <int> func = XXX; Debug. log (func ();} int XXX () {return 10 ;}}
 
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 the result returned by Func <TResult> delegate ";}
 
Predicate can only accept one input parameter. The returned value is of the bool type.
# Region Predicate // bool Predicate <T> usage // enter a T-type parameter, return Value: Boolean Predicate <string []> predicate = delegate (string [] x) {var result = from p in x where p. 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 used to determine whether the String array contains s items. If yes, the They contain will be printed on the console. If no, the They don't contain will be printed.
 
// Define public void CallUI <T> (Action <T, object []> callback, params object [] args) where T: CUIBase // call CUIManager. instance. callUI <CUIMidMsg> (_ ui, _ arg) => _ ui. showMsg (string) _ arg [0]), string. format (szMsg, format ));
 

Part of the Reference: Fengyu Chong Unity3D tutorial Institute


The copyright of this article is shared by the author and the blog Park, source URL: http://www.cnblogs.com/zhaoqingqing/. You are welcome to reprint, but without the author's consent, reprinted after the articleThe author and the original article must be clearly connected on the article page.Otherwise, you are entitled to pursue legal liability.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.