Func, Action, Predict
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Event
{
class Program
{
static void Main(string[] args)
{
委托:
//MyDelegate<int> addDelegate = null;
//addDelegate += Add;
//Console.WriteLine(addDelegate(1, 2));
//Console.ReadKey();
//Lambda表达式:
//第一种
//MyDelegate<int> myDelegate = null;
//myDelegate += (int a, int b) => a + b;
//第二种
//MyDelegate<int> myDelegate = (int a, int b) => a + b;
//第三种
//MyDelegate<int> myDelegate = (int a, int b) => { return a + b; };
//Console.WriteLine(myDelegate(1, 2));
//Func委托
//Func<int, int, double> func = Add;
//double result = func(10, 20);
//Console.WriteLine(result);
//Func<int, int, double> func = (a, b) => a + b;
//Console.WriteLine(func(10, 20));
//Console.ReadKey();
//Func委托进阶
//int[] nums = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
//Console.WriteLine(CommonMethod(Add,nums,0,3)); // 0, 3是数组索引 :34
//Console.WriteLine(CommonMethod(Mutiply, nums, 0, 5)); //151200
//Console.ReadKey();
//Func 委托必须要求所接收的方法有一个返回值. 如果没有返回值,可以使用Action委托
//Action委托接收一个没有返回值的方法
//Action委托
//Action<string> act = (a) => Console.WriteLine("{0}欢迎你",a);
//act("c");
//Console.ReadKey();
//predict委托
/*
* Predicate<T>委托定义如下:
* public delegate bool Predicate<T>(T obj);
* 解释:此委托返回一个bool值的方法
* 在实际开发中,Predicate<T>委托变量引用一个“判断条件函数”,
* 在判断条件函数内部书写代码表明函数参数所引用的对象应该满足的条件,条件满足时返回true
*/
List<Student> stuList = new List<Student>()
{
new Student(){ StudentId=1001,StudentName="小张"},
new Student(){ StudentId=1008,StudentName="小李"},
new Student(){ StudentId=1009,StudentName="小王"},
new Student(){ StudentId=1003,StudentName="小赵"},
new Student(){ StudentId=1005,StudentName="小刘"}
};
//查询学号大于1003的学员
//List<T>集合中定义了一个FindAll方法:public T FindAll(Predicate<T> match)
List<Student> list = stuList.FindAll(a => a.StudentId >= 1005);//传入的参数a是Student类型
foreach (Student item in list)
{
Console.WriteLine(item.StudentName);
}
Console.ReadKey();
}
static int Add(int a, int b)
{
return a + b;
}
static int Mutiply(int i, int j)
{
return i * j;
}
static double Sub(double a,double b)
{
return a - b;
}
//使用func委托,将"运算本身"作为方法参数
static int CommonMethod(Func<int, int, int> opration, int[] nums, int a, int b)
{
int result = nums[a];//把第一个值,作为基数(a+1:表示下一个值)
for (int i = a+1; i <= b; i++)
{
result = opration(result,nums[i]);
}
return result;
}
}
public delegate T MyDelegate<T>(T obj1, T obj2);
}
//【1】Func系列委托:多个重载版本
//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 delegate TResult Func<T1,T2,T3,TResult>(T1 arg1,T2 arg2,T3 arg3);
//public delegate TResult Func<T1,T2,T3,T4,TResult>(T1 arg1,T2 arg2,T3 arg3,T4 args);
/*
* Action委托与Func系列类似,有若干个重载方法...,可以接收0-4个参数,且返回值为void类型的方法
* public delegate void Action();
* public delegate void Action<T>(T obj);
* public delegate void Action<T1,T2>(T1 arg1,T2 arg2);
* public delegate void Action<T1,T2,T3>(T1 arg1,T2 arg2,T3 arg3);
* public delegate void Action<T1,T2,T3,T4>(T1 arg1,T2 arg2,T3 arg3,T4 arg4);
*
*
*/
/*
*【注意问题】
* Func委托声明的最后一个泛型类型参数是委托所接收方法的《返回值类型》
* 如果前面有泛型类型的参数,这个参数就是委托方法的形参类型
*
*【牢记内容】
* Func委托系列引用一个《有返回值的方法》,也就是将方法作为另一个方法的“参数”
*/