|
|
|
@ -23,13 +23,7 @@ namespace Study.DelegateSeries.Core
|
|
|
|
|
* 10、总结
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#region 定义委托
|
|
|
|
|
//基本语法
|
|
|
|
|
public delegate decimal OperationDelegate(decimal a,decimal b);
|
|
|
|
|
|
|
|
|
|
public delegate string ShowTypeNameDelegate();
|
|
|
|
|
|
|
|
|
|
public delegate string ShowNameDelegate();
|
|
|
|
|
#region 定义委托,DeclareDelegate类
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 实例化委托
|
|
|
|
@ -38,9 +32,9 @@ namespace Study.DelegateSeries.Core
|
|
|
|
|
/// 基础语法
|
|
|
|
|
/// (c#1.0 开始提供)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OperationDelegate BaseInstance()
|
|
|
|
|
public CalculatorDelegate BaseInstance()
|
|
|
|
|
{
|
|
|
|
|
OperationDelegate operation = new OperationDelegate(new Addition().Add);
|
|
|
|
|
CalculatorDelegate operation = new CalculatorDelegate(new Addition().Add);
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -48,9 +42,9 @@ namespace Study.DelegateSeries.Core
|
|
|
|
|
/// c#2.0 简写方法
|
|
|
|
|
/// (基于类型推断)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OperationDelegate AutoTypeInstance()
|
|
|
|
|
public CalculatorDelegate AutoTypeInstance()
|
|
|
|
|
{
|
|
|
|
|
OperationDelegate operation = new Subtraction().Difference;
|
|
|
|
|
CalculatorDelegate operation = new Subtraction().Difference;
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -58,10 +52,10 @@ namespace Study.DelegateSeries.Core
|
|
|
|
|
/// c#2.0 匿名委托
|
|
|
|
|
/// (使用匿名方法实例化的委托)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OperationDelegate AnonymousInstance()
|
|
|
|
|
public CalculatorDelegate AnonymousInstance()
|
|
|
|
|
{
|
|
|
|
|
//c# 2.0基础语法
|
|
|
|
|
OperationDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); };
|
|
|
|
|
CalculatorDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); };
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -69,14 +63,14 @@ namespace Study.DelegateSeries.Core
|
|
|
|
|
/// c#3.0 Lambda表达式
|
|
|
|
|
/// (实质:"Lambda表达式"是一个匿名函数)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OperationDelegate LambdaInstance()
|
|
|
|
|
public CalculatorDelegate LambdaInstance()
|
|
|
|
|
{
|
|
|
|
|
OperationDelegate operation = (decimal a,decimal b)=> { return a * a + b * b + 2 * a * b; };
|
|
|
|
|
CalculatorDelegate operation = (decimal a,decimal b)=> { return a * a + b * b + 2 * a * b; };
|
|
|
|
|
//自动推断
|
|
|
|
|
OperationDelegate operation2 = (a,b) => { return a * a + b * b + 2 * a * b; };
|
|
|
|
|
CalculatorDelegate operation2 = (a,b) => { return a * a + b * b + 2 * a * b; };
|
|
|
|
|
|
|
|
|
|
//{}只有一句时的简写
|
|
|
|
|
OperationDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b ;
|
|
|
|
|
CalculatorDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b ;
|
|
|
|
|
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|