using Study.DelegateSeries.Core.Calculator;
using System;
namespace Study.DelegateSeries.Core
{
///
/// 委托学习
/// 使用:定义委托、实例化委托、调用委托
///
public class StudyDelegate
{
/*
* 委托系列学习
* 1、委托概念
* 2、定义与使用委托
* 3、多播委托
* 4、使用匿名方法
* 5、使用Lamda表达式
* 6、委托使用场景
* 7、委托与事件
* 8、委托与接口
* 9、其它
* 10、总结
*/
#region 定义委托
//基本语法
public delegate decimal OperationDelegate(decimal a,decimal b);
public delegate string ShowTypeNameDelegate();
public delegate string ShowNameDelegate();
#endregion
#region 实例化委托
///
/// 基础语法
/// (c#1.0 开始提供)
///
public OperationDelegate BaseInstance()
{
OperationDelegate operation = new OperationDelegate(new Addition().Add);
return operation;
}
///
/// c#2.0 简写方法
/// (基于类型推断)
///
public OperationDelegate AutoTypeInstance()
{
OperationDelegate operation = new Subtraction().Difference;
return operation;
}
///
/// c#2.0 匿名委托
/// (使用匿名方法实例化的委托)
///
public OperationDelegate AnonymousInstance()
{
//c# 2.0基础语法
OperationDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); };
return operation;
}
///
/// c#3.0 Lambda表达式
/// (实质:"Lambda表达式"是一个匿名函数)
///
public OperationDelegate LambdaInstance()
{
OperationDelegate 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; };
//{}只有一句时的简写
OperationDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b ;
return operation;
}
#endregion
#region 调用委托
#endregion
#region 闭包
#endregion
#region 多播委托
#endregion
#region 泛型委托
#endregion
#region 内置委托
#endregion
#region 委托与事件
#endregion
#region 委托与接口
#endregion
}
}