成都创新互联网站制作重庆分公司

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

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

十年的环翠网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整环翠建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“环翠网站设计”,“环翠网站推广”以来,每个客户项目都认真落实执行。

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 obj);
public delegate void Action(T1 arg1, T2 arg2);
public class ActionTest : MonoBehaviour {
    public MyDelegate myDelegate; //使用委托

    Action action;
    Action action1;
    Action action2;
    Action 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();
public delegate TResult Func(T arg);
public delegate TResult Func(T1 arg1, T2 arg2);

public class FuncTest : MonoBehaviour
{
    Func func;
    Func func1;
    Predicate predicate;
    void Start()
    {
        func = XXX;
        Debug.Log(func());
        func1 = CallStringLength;
        Debug.Log(func1("sadasdads"));

        ///bool Predicate的用法
        ///输入一个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怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站栏目:c#中Delegate,Action,Func和Predicate怎么使用
当前路径:http://cxhlcq.com/article/igedji.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部