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.
61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using Study.DelegateSeries.Core.Calculator;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Study.DelegateSeries.Core
|
|
{
|
|
/// <summary>
|
|
/// 实例化委托
|
|
/// </summary>
|
|
public class InstanceDelegate
|
|
{
|
|
/// <summary>
|
|
/// 基础语法
|
|
/// (c#1.0 开始提供)
|
|
/// </summary>
|
|
public CalculatorDelegate BaseInstance()
|
|
{
|
|
CalculatorDelegate operation = new CalculatorDelegate(new Addition().Add);
|
|
return operation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// c#2.0 简写方法
|
|
/// (基于类型推断)
|
|
/// </summary>
|
|
public CalculatorDelegate AutoTypeInstance()
|
|
{
|
|
CalculatorDelegate operation = new Subtraction().Difference;
|
|
return operation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// c#2.0 匿名委托
|
|
/// (使用匿名方法实例化的委托)
|
|
/// </summary>
|
|
public CalculatorDelegate AnonymousInstance()
|
|
{
|
|
//c# 2.0基础语法
|
|
CalculatorDelegate operation = delegate (decimal a, decimal b) { return (a * b) * (a * b); };
|
|
return operation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// c#3.0 Lambda表达式
|
|
/// (实质:"Lambda表达式"是一个匿名函数)
|
|
/// </summary>
|
|
public CalculatorDelegate LambdaInstance()
|
|
{
|
|
CalculatorDelegate operation = (decimal a, decimal b) => { return a * a + b * b + 2 * a * b; };
|
|
//自动推断
|
|
CalculatorDelegate operation2 = (a, b) => { return a * a + b * b + 2 * a * b; };
|
|
|
|
//{}只有一句时的简写
|
|
CalculatorDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b;
|
|
|
|
return operation;
|
|
}
|
|
}
|
|
}
|