Action/Func/Lamda/匿名委托

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Test : MonoBehaviour
{
    public delegate void TestDelegate();
    public Action TestAction;
    public Func TestFunc;


    private void Start()
    {
        TestDelegate testDelegate = () => Debug.LogError("This is Delegate Test");

        testDelegate += delegate () { Debug.LogError("This is AnonymityTest"); };

        TestAction = () => Debug.LogError("This is Action Test");

        TestFunc = () => "This is Func Test";

        testDelegate();

        TestAction()

        Debug.LogError(TestFunc());


        TestLambda(() => Debug.LogError("This is ActionParamTest"));

        TestLambda(() =>"This is FuncParamTest");

        TestAnonymityLambda(delegate () { Debug.LogError("This is AnonymityTest"); });
    }


    public void TestLambda(Func func)
    {
        if (func != null)
            Debug.LogError(func());
    }


    public void TestLambda(Action action)
    {
        if (action != null)
            action();
    }


    public void TestAnonymityLambda(TestDelegate testDelegate)
    {
        if (testDelegate != null)
            testDelegate();
    }


}

你可能感兴趣的:(Action/Func/Lamda/匿名委托)