C# 2.0学习之--匿名代理

翻译 2006年06月23日 18:09:00

 

 

编译与执行:

csc AnonymousDelegates.cs
AnonymousDelegates


 

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中采用了匿名方法,它允许我们能以一种直观的方式理解委托。匿名方法允许我们直接对一个委托对象定义代码段。当我们创建一个仅有小段代码的委托的时候,匿名方法提供了更为灵活的解决之路。让我们来看看...

(多年后回头学习c#)被遗忘的世界 一 匿名类 与 扩展方法

1.1  匿名类                 1 、var  与 new 一起使用 创建匿名类 ...

C#封装、多态、抽象、接口、匿名方法等学习

1:封装    将对象进行封装,并不等于将整个对象完全包裹起来,而是根据实际需要,设置一定的访问权限,用户根据不同的权限调用对象提供的功能,在C#语言中,可以使用修饰符public、internal...

C#学习日记25---匿名方法 与 Func委托 与 lambda表达式

在 2.0 之前的 C# 版本中,声明委托的唯一方法是使用命名方法。C# 2.0 引入了匿名方法(委托),而在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方...
  • HC666
  • HC666
  • 2015-10-18 14:35
  • 2503

[深入学习C#]匿名函数、委托和Lambda表达式

匿名函数  匿名函数(Anonymous Function)是表示“内联”方法定义的表达式。匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情)。匿名函数转换的计算...

C#中新特性的学习:Delegate、匿名方法、lambda表达式

最近在修改一个项目的代码中,用到了lambda表达式的写法,就顺便学习了一下,发现弄清楚lambda必须要弄清楚匿名方法,弄清楚匿名方法必须要弄清楚Delegate,弄清楚Delegate还要理解C+...

C++与C#对比学习:内联函数,匿名函数

为啥用内联函数(inline functions)      在C++中函数调用需要建立栈环境,进行参数复制,保护调用现场,返回时进行返回值复制,恢复调用现场.这些操作都需要额外开销.那有啥好办法...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)