/*
* 委托的取值与清空委托列表
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A12_GetAndCleanDelegateList
{
class Demo9
{
private Action actHandler;
public Demo9()
{
actHandler += DelegateMethod1;
actHandler += DelegateMethod2;
actHandler += DelegateMethod3;
}
public void DelegateMethod1()
{
Console.WriteLine("Method DelegateMethod1");
}
public void DelegateMethod2()
{
Console.WriteLine("Method DelegateMethod2");
}
public void DelegateMethod3()
{
Console.WriteLine("Method DelegateMethod3");
}
//取得委托列表
public void Test1()
{
Delegate[] delArray = actHandler.GetInvocationList();
foreach (Delegate item in delArray)
{
item.DynamicInvoke();
}
}
//清空委托列表
public void Test2()
{
while (actHandler!=null)
{
actHandler -= this.actHandler;
}
if (actHandler != null)
{
actHandler.Invoke();
}
else
{
Console.WriteLine("委托列表已经清空");
}
}
//清空委托列表通用算法
public void Test3()
{
Delegate[] delArray = actHandler.GetInvocationList();
for (int i = 0; i < delArray.Length; i++)
{
actHandler -= delArray[i] as Action;
}
if (actHandler != null)
{
actHandler.Invoke();
}
else
{
Console.WriteLine("Demo9:Test3() 委托列表已经清空");
}
}
static void Main1(string[] args)
{
Demo9 obj = new Demo9();
obj.Test1();
//obj.Test2();
obj.Test3();
}
}
}
/*
* 匿名委托
* 如果一个委托不需要在别处调用,那么使用匿名方法可以减少代码量
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A12_GetAndCleanDelegateList
{
class Demo10
{
public void Test2()
{
Func<int, int, int> funAddingMethodHandler = delegate (int num1, int num2)
{
return num1 + num2;
};
int result = funAddingMethodHandler(20, 30);
Console.WriteLine(result);
}
static void Main1(string[] args)
{
Demo10 obj = new Demo10();
obj.Test2();
}
}
}
/*
* Lambda表达式
* 对于匿名委托的进一步简化
* 1.去掉delegate关键字
* 2.去掉参数列表中的类型定义
* 3.如果方法中只有一条语句,可以去掉return和{};
* 4.
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A12_GetAndCleanDelegateList
{
class Demo11
{
public void Test1()
{
//匿名委托
Func<int, int, int> funAddingMethodHandler = delegate (int num1, int num2)
{
return num1 + num2;
};
//Lambda表达式
Func<int, int, int> funAddingMethodHandler2 = (int num1, int num2) =>
{
return num1 + num2;
};
Func<int, int, int> funAddingMethodHandler3 = (num1, num2) =>
{
return num1 + num2;
};
Func<int, int, int> funAddingMethodHandler4 = (num1, num2) => num1 + num2;
//方法中只有一个参数,可以去掉()
Func<int, int> funAddingMethodHandler5 = (num1) => num1 + 888;
Func<int, int> funAddingMethodHandler6 = num1 => num1 + 888;
//如果没有参数
Func<int> funAddingMethodHandler7 = () => 888;
Console.WriteLine(funAddingMethodHandler(10, 20));
Console.WriteLine(funAddingMethodHandler2(80, 20));
Console.WriteLine(funAddingMethodHandler3(80, 30));
Console.WriteLine(funAddingMethodHandler4(80, 40));
Console.WriteLine(funAddingMethodHandler5(200));
Console.WriteLine(funAddingMethodHandler6(20));
Console.WriteLine(funAddingMethodHandler7());
}
//Lambda表达式使用外部变量
public void Test2()
{
int num = 100;
Func<int, int> funHandler = x => x + num; // 其中num表达式外部变量
Console.WriteLine(funHandler(1000));
}
static void Main1(string[] args)
{
Demo11 obj = new Demo11();
obj.Test1();
obj.Test2();
}
}
}