using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LambdaTest
{
public class LambdaCreate
{
//自定义委托(Action和Func实质上是封装好的委托)
//将委托封装成事件来注册,则可以一个事件注册多个同类型方法(事件执行将调用全部已注册过的方法)
delegate void delegate_action1();
event delegate_action1 delegate_action1_event;
delegate void delegate_action2(object a,object b);
event delegate_action2 delegate_action2_event;
delegate object delegate_func1(object a);
event delegate_func1 delegate_func1_event;
delegate object delegate_func2(object a,object b);
event delegate_func2 delegate_func2_event;
//Action和Func实质上是封装好的委托
//自定义泛型委托(多写几个重载的话可替代Action和Func)
delegate void delegate_func3<T>(T a, T b);
delegate T delegate_func4<T>(T a, T b);
public void Test()
{
//除事件外,被定义封装的委托可当成参数传递使用
//有16个函数重载,最多的有16个形参
Action action1 = () => { int c = 8 + 9; };
Action<int, int> action2 = (int a, int b) => { int c = a + b; };
Action<object, object> action3 = (object a, object b) => { a = b; };
//有16个函数重载,最多的有16个形参
Func<int> func1 = () => { int c = 1; return c; };//Fun至少要有一个返回参数,输入形参可没有
Func<int, int> func2 = (int a) => { int c = a; return c; };
Func<object, object> func3 = (object a) => { object c = a; return c; };//最后一个形参是返回类型
//自定义委托,可替代Action和Func(Action和Func实质上是封装好的委托)
delegate_action1_event += new delegate_action1(() => { int c = 8 + 9; });
delegate_action2_event += new delegate_action2((object a,object b) => { object c = a; c = b; });
delegate_func1_event += new delegate_func1((object a) => { object c = 9;return c; });
delegate_func2_event += new delegate_func2((object a,object b) => { object c = a; return c; });
//以上部分方法的lambda表达式写法
action1 = new Action(() => { int c = 8 + 9; });
action3 = new Action<object,object>((object a, object b) => { a = b; });
func1 = new Func<int>(()=> { int c = 1; return c; }) ;
func3 = new Func<object, object>((object a) => { object c = a; return c; });
delegate_action1 normal_delegate_action1 = new delegate_action1(() => { int a = 9; });
delegate_action2 normal_delegate_action2 = new delegate_action2((object a,object b) => { a = b; });
delegate_func1 normal_delegate_func1 = new delegate_func1((object a) => { object c = a; return c; });
delegate_func2 normal_delegate_func2 = new delegate_func2((object a,object b) => { object c = a; return b; });
normal_delegate_action1 = () => { int a = 9; };
normal_delegate_action2 = (object a, object b) => { a = b; };
normal_delegate_func1 = (object a) => { object c = a; return c; };
normal_delegate_func2 = (object a, object b) => { object c = a; return b; };
delegate_action1_event += () => { int c = 8 + 9; };
delegate_action2_event += (object a, object b) => { object c = a; c = b; };
delegate_func1_event += (object a) => { object c = 9; return c; };
delegate_func2_event += (object a, object b) => { object c = a; return c; };
//泛型声明的时候是T,定义实例的时候需要填具体的参数类型(而不是T或T1,T2)
delegate_func3<int> normal_delegate_func3 = new delegate_func3<int>((int a,int b) => { int c = a + b; });
delegate_func4<object> normal_delegate_func4 = new delegate_func4<object>((object a, object b) => { object c = a; return b; });
}
}
}