You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.7 KiB
C#

6 years ago
using Study.DelegateSeries.Core.Calculator;
using System;
6 years ago
namespace Study.DelegateSeries.Core
{
/// <summary>
/// 委托学习
6 years ago
/// 使用:定义委托、实例化委托、调用委托
6 years ago
/// </summary>
public class StudyDelegate
{
/*
*
* 1
* 2使
* 3
* 4使
* 5使Lamda
* 6使
* 7
* 8
* 9
* 10
*/
6 years ago
#region 定义委托
//基本语法
public delegate decimal OperationDelegate(decimal a,decimal b);
public delegate string ShowTypeNameDelegate();
public delegate string ShowNameDelegate();
#endregion
#region 实例化委托
/// <summary>
/// 基础语法
/// (c#1.0 开始提供)
/// </summary>
public OperationDelegate BaseInstance()
{
OperationDelegate operation = new OperationDelegate(new Addition().Add);
return operation;
}
/// <summary>
/// c#2.0 简写方法
/// (基于类型推断)
/// </summary>
public OperationDelegate AutoTypeInstance()
{
OperationDelegate operation = new Subtraction().Difference;
return operation;
}
/// <summary>
/// c#2.0 匿名委托
/// (使用匿名方法实例化的委托)
/// </summary>
public OperationDelegate AnonymousInstance()
{
//c# 2.0基础语法
OperationDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); };
return operation;
}
/// <summary>
/// c#3.0 Lambda表达式
/// (实质:"Lambda表达式"是一个匿名函数)
/// </summary>
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
6 years ago
}
}