C#中的Action和Func和Predicate

一、【action<>】指定那些只有输入参数,没有返回值的委托

用了Action之后呢:

就是相当于省去了定义委托的步骤了。

演示代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventDemo
{
    class Program
    {
        public delegate void myDelegate(string str);  
        public static void HellowChinese(string strChinese)  
        {  
            Console.WriteLine("Good morning," + strChinese);  
            Console.ReadLine();  
        }  

        static void Main(string[] args)
        {
            //Delegate的代码
            myDelegate d = new myDelegate(HellowChinese);
            d("Mr wang");

            //用了Action之后呢
            Action<string> action = HellowChinese;
            action("Spring.");

            Console.ReadLine();
        }
    }
}
View Code

 

二、func<> 这个和上面的那个是一样的,区别是这个有返回值!

语法:

Func<参数,返回值>变量名=函数名 

Lambda表达式的调用方式
语法:(显示类型的参数列表)=>{语句}
eg:
Func<int,int,string>func=(x,y)=>(x*y).Tostring();
Console.WriteLine(fun(5,20));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace EventDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //类似委托功能
            Func<string, int> test = TsetMothod;
            Console.WriteLine(test("123"));
            Func<string, int> test1 = TsetMothod;


            //只需要调用这个类就可以减少重复的代码
            CallMethod<string>(test1, "123");
            //或者采用这种
            CallMethod<string>(new Func<string, int>(TsetMothod), "123");
            CallMethod(new Func<string, int>(TsetMothod), "123");

            Func<int, double, decimal, string> testFun = TestFun;
            double b = 2.3;
            decimal c = 666.7m;
            string strtestFun = testFun(1, b, c);
            Console.WriteLine("Func<int, double, decimal, string> testFun={0}", strtestFun);

            Console.ReadKey();
        }

        public static string TestFun(int a, double b, decimal c)
        {
            return "TestFun";
        }

        public static int TsetMothod(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return 1;
            }
            return 0;
        }

        public static void CallMethod<T>(Func<T, int> func, T item)
        {
            try
            {
                int i = func(item);
                Console.WriteLine(i);
            }
            catch (Exception e)
            {

            }
            finally
            {

            }
        }
    }
}
View Code
Predicate 泛型委托
  表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
 
public delegate bool Predicate<T>(T obj);
类型参数介绍:
   T: 要比较的对象的类型。
   obj: 要按照由此委托表示的方法中定义的条件进行比较的对象。
   返回值:如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false。
看下面代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>() { "Mike", "Rose", "Steve" };
            var mike = list.Find(new Predicate<string>(HaveLengthFive));
            Console.WriteLine(mike);
            Console.ReadLine();
        }
        static bool HaveLengthFive(string value)
        {
            return value.Length == 5;
        }
    }
}
View Code

延伸:
  除了上面提到的外,你完全可以使用Predicate 定义新的方法,来加强自己代码。

public class GenericDelegateDemo
{
    List<String> listString = new List<String>()
    {
        "One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
    };

    public String GetStringList(Predicate<String> p)
    {
        foreach(string item in listString)
        {
            if (p(item))
                return item;
        }
        return null;
    }

    public bool ExistString()
    {
        string str = GetStringList((c) => { return c.Length <= 3 && c.Contains('S'); });
        if (str == null)
            return false;
        else
            return true;
    }
}
View Code

 

 
 

转载于:https://www.cnblogs.com/scmail81/p/8678798.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值