1.Func有返回值的系统类型的泛型委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiketang.com.AdvanceTeach
{
/// <summary>
/// 有返回值的系统类型的泛型委托
///
/// 目的:为了方法开发者,.NET基类库中针对常用的情况,提供了预订好委托。这些委托使用非常广泛。
/// </summary>
class FunDelegateDemo
{
#region 【1】Func委托的基本使用
public void Test1()
{
//【1】Func关联具体方法
Func<int, int, double> myFunc1 = Add1;
//【2】Func直接使用Lambda表达式
Func<int, int, double> myFunc2 = (a, b) => a + b;
Console.WriteLine("Func的使用:" + myFunc2(10, 20));
}
private double Add1(int a, int b)
{
return a + b;
}
#endregion
#region 【2】Func委托的重要使用
//问题:给定一个数组 { 10, 9, 8, 7, 6, 5, 4, 3, 2 },从数组中指定位置抽取3个数,求和、求积
//思考:我们运算的要求不一样(求和、求积),两种运算可以单独做为方法
public int Sum(int[] nums, int from, int to)
{
int result = 0;
for (int i = from; i <= to; i++)
{
result += nums[i];
}
return result;
}
public int Mul(int[] nums, int from, int to)
{
int result = 1;
for (int i = from; i <= to; i++)
{
result *= nums[i];
}
return result;
}
//还可以继续增加其他的方法...
public void Test2()
{
int[] nums = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
//【1】普通的方法
int resul1 = Sum(nums, 0, 2);
Console.WriteLine(" 10, 9, 8 求和=" + resul1);
int result2 = Mul(nums, 0, 2);
Console.WriteLine(" 10, 9, 8 求积=" + result2);
}
//使用Func委托,将“运算”本身作为方法参数
public int Operation(Func<int, int, int> method, int[] nums, int from, int to)
{
int result = nums[from]; //把第一个值作为一个基数 from + 1表示下一个值
for (int i = from + 1; i <= to; i++)
{
result = method(result, nums[i]);
}
return result;
}
static int Add2(int a, int b)
{
return a + b;
}
static int Multiply(int a, int b)
{
return a * b;
}
public void Test3()
{
int[] nums = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
int resul1 = Operation(Add2, nums, 0, 2);
Console.WriteLine(" 10, 9, 8 求和=" + resul1);
int result2 = Operation(Multiply, nums, 0, 2);
Console.WriteLine(" 10, 9, 8 求积=" + result2);
Console.WriteLine("----------------------------------------------");
//【2】将Func作为方法参数,结合Lambda表达式
int resul3 = Operation((a,b)=>a+b, nums, 0, 2);
Console.WriteLine(" 10, 9, 8 求和=" + resul3);
int result4 = Operation((a, b) => a *b, nums, 0, 2);
Console.WriteLine(" 10, 9, 8 求积=" + result4);
}
#endregion
}
}
2.Action系统泛型委托,没有参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiketang.com.AdvanceTeach
{
/// <summary>
/// Action系统泛型委托
///
/// 和Func相比,Action没有参数,所以:Action委托接收的是一个没有返回值的方法
///
/// </summary>
class ActionDelegateDemo
{
public void Test()
{
Action<string> action = (name) => Console.WriteLine($"欢迎参加【{name}】的VIP课程");
action("常老师");
}
}
/*
* Action委托与Func系列类似,有若干个重载方法...,且返回值为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);
* ....
*
*/
}
3.Lambda表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiketang.com.AdvanceTeach
{
/// <summary>
/// 委托--》匿名方法--》Lambda表达式
///
/// 委托:委托是方法的变量,是可以代表一个或多个方法!
/// 思考:委托能不能做为方法参数?Yes!按照多态的理论:委托作为方法参数,实际传递的是具体方法!
///
/// </summary>
class LambdaDemo
{
public void Test()
{
//【1】委托关联独立的方法
CalculatorDelegate cal1 = Add;
//【2】委托关联匿名方法
CalculatorDelegate cal2 = delegate (int a, int b)
{
return a - b;
};
//【3】将匿名方法用Lambda表达式简化编写goes to
CalculatorDelegate cal3 = (int a, int b) => { return a + b; };
Console.WriteLine("通过匿名方法计算:20-30=" + cal2(20, 30));
//进一步简化(假如,方法中只有一行代码)
CalculatorDelegate cal4 = (a, b) => a - b;
SayHelloDelegate sayHello = () => "欢迎大家参加常老师主讲的VIP课程";
Console.WriteLine("通过Lambda表达式计算:20-30=" + cal4(20, 30));
}
private int Add(int a, int b)
{
return a + b;
}
//根据委托的使用可以继续增加其他的方法...
}
public delegate int CalculatorDelegate(int a, int b);
public delegate string SayHelloDelegate();
}