温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c#中Delegate,Action,Func和Predicate怎么使用

发布时间:2022-10-24 11:30:53 来源:亿速云 阅读:116 作者:iii 栏目:编程语言

这篇文章主要介绍“c#中Delegate,Action,Func和Predicate怎么使用”,在日常操作中,相信很多人在c#中Delegate,Action,Func和Predicate怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”c#中Delegate,Action,Func和Predicate怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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;//无参数
        action();

        action1 += test2;//一个参数
        action1("Test2");

        action2 += test3;//两个参数
        action2("Test3", 99);

        action3 += test4;//集合参数
        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);
        }
    }
}

2.Func,Predicate

Func可以传入多个参数,默认最后一个为返回值
Predicate只能接受一个传入参数,返回值为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>的用法
        ///输入一个T类型的参数,返回值为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;
    }
}

到此,关于“c#中Delegate,Action,Func和Predicate怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI