C# 2.0学习之--匿名代理
来源:互联网 发布:mac散热声音大怎么办 编辑:程序博客网 时间:2024/05/16 00:58
编译与执行:
csc AnonymousDelegates.csAnonymousDelegates
AnonymousDelegates.cs :
//Copyright (C) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
namespace AnonymousDelegate_Sample
{
// Define the delegate method.
delegate decimal CalculateBonus(decimal sales);
// Define an Employee type.
class Employee
{
public string name;
public decimal sales;
public decimal bonus;
public CalculateBonus calculation_algorithm;
}
class Program
{
// This class will define two delegates that perform a calculation.
// The first will be a named method, the second an anonymous delegate.
// This is the named method.
// It defines one possible implementation of the Bonus Calculation algorithm.
static decimal CalculateStandardBonus(decimal sales)
{
return sales / 10;
}
static void Main(string[] args)
{
// A value used in the calculation of the bonus.
// Note: This local variable will become a "captured outer variable".
decimal multiplier = 2;
// This delegate is defined as a named method.
CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);
// This delegate is anonymous - there is no named method.
// It defines an alternative bonus calculation algorithm.
CalculateBonus enhanced_bonus = delegate(decimal sales) { return multiplier * sales / 10; };
// Declare some Employee objects.
Employee[] staff = new Employee[5];
// Populate the array of Employees.
for (int i = 0; i < 5; i++)
staff[i] = new Employee();
// Assign initial values to Employees.
staff[0].name = "Mr Apple";
staff[0].sales = 100;
staff[0].calculation_algorithm = standard_bonus;
staff[1].name = "Ms Banana";
staff[1].sales = 200;
staff[1].calculation_algorithm = standard_bonus;
staff[2].name = "Mr Cherry";
staff[2].sales = 300;
staff[2].calculation_algorithm = standard_bonus;
staff[3].name = "Mr Date";
staff[3].sales = 100;
staff[3].calculation_algorithm = enhanced_bonus;
staff[4].name = "Ms Elderberry";
staff[4].sales = 250;
staff[4].calculation_algorithm = enhanced_bonus;
// Calculate bonus for all Employees
foreach (Employee person in staff)
PerformBonusCalculation(person);
// Display the details of all Employees
foreach (Employee person in staff)
DisplayPersonDetails(person);
}
public static void PerformBonusCalculation(Employee person)
{
// This method uses the delegate stored in the person object
// to perform the calculation.
// Note: This method knows about the multiplier local variable, even though
// that variable is outside the scope of this method.
// The multipler varaible is a "captured outer variable".
person.bonus = person.calculation_algorithm(person.sales);
}
public static void DisplayPersonDetails(Employee person)
{
Console.WriteLine(person.name);
Console.WriteLine(person.bonus);
Console.WriteLine("---------------");
}
}
}
- C# 2.0学习之--匿名代理
- C# 2.0学习之--代理1
- C# 2.0学习之--代理2
- 代理之匿名函数
- C#匿名代理的使用
- C#2.0学习7--匿名方法,迭代器
- c#学习笔记-匿名
- 【C#学习】匿名方法
- C#学习之代理委托的使用
- C#3.0之匿名类型
- C#2.0 匿名委托
- C#2.0匿名函数
- C#2.0匿名委托
- C#2.0 新特性探究之委托与匿名委托
- C#代理delegata学习
- Unity3D学习之C#的代理和事件
- Unity3D学习之如何利用C#实现代理模式
- C#设计模式学习小结之十 代理模式
- Vs.net2005制作安装布署程序是的问题总汇
- 用Office玩中文分词
- C语言中一种更优雅的异常处理机制
- 宽带路由器
- 我错了吗?
- C# 2.0学习之--匿名代理
- C# 2.0学习之--数组
- 好心情,好收获
- C# 2.0学习之--集合
- 开始写Win32 Application程序了
- C# 2.0学习之--集合2
- 中断方式实现发送数据串口驱动
- 有用的网址
- C# 2.0学习之--条件编译