[C#] C#中委托用法演示代码 →→→→→进入此内容的聊天室

来自 , 2019-05-17, 写在 C#, 查看 107 次.
URL http://www.code666.cn/view/d1f44e2f
  1. // 版权所有 (C) Microsoft Corporation。保留所有权利。
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace AnonymousDelegate_Sample
  8. {
  9.  
  10.     // 定义委托方法。
  11.     delegate decimal CalculateBonus(decimal sales);
  12.  
  13.     // 定义一个 Employee 类型。
  14.     class Employee
  15.     {
  16.         public string name;
  17.         public decimal sales;
  18.         public decimal bonus;
  19.         public CalculateBonus calculation_algorithm;
  20.     }
  21.  
  22.     class Program
  23.     {
  24.  
  25.         // 此类将定义两个执行计算的委托。
  26.         // 第一个是命名方法,第二个是匿名委托。
  27.  
  28.         // 首先是命名方法。
  29.         // 该方法定义“奖金计算”算法的一个可能实现。
  30.  
  31.         static decimal CalculateStandardBonus(decimal sales)
  32.         {
  33.             return sales / 10;
  34.         }
  35.  
  36.         static void Main(string[] args)
  37.         {
  38.  
  39.             // 奖金计算中用到的值。
  40.             // 注意:此局部变量将变为“捕获的外部变量”。
  41.             decimal multiplier = 2;
  42.  
  43.             // 将此委托定义为命名方法。
  44.             CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);
  45.  
  46.             // 此委托是匿名的,没有命名方法。
  47.             // 它定义了一个备选的奖金计算算法。
  48.             CalculateBonus enhanced_bonus = delegate(decimal sales) { return multiplier * sales / 10; };
  49.  
  50.             // 声明一些 Employee 对象。
  51.             Employee[] staff = new Employee[5];
  52.  
  53.             // 填充 Employees 数组。
  54.             for (int i = 0; i < 5; i++)
  55.                 staff[i] = new Employee();
  56.  
  57.             // 将初始值赋给 Employees。
  58.             staff[0].name = "Mr Apple";
  59.             staff[0].sales = 100;
  60.             staff[0].calculation_algorithm = standard_bonus;
  61.  
  62.             staff[1].name = "Ms Banana";
  63.             staff[1].sales = 200;
  64.             staff[1].calculation_algorithm = standard_bonus;
  65.  
  66.             staff[2].name = "Mr Cherry";
  67.             staff[2].sales = 300;
  68.             staff[2].calculation_algorithm = standard_bonus;
  69.  
  70.             staff[3].name = "Mr Date";
  71.             staff[3].sales = 100;
  72.             staff[3].calculation_algorithm = enhanced_bonus;
  73.  
  74.             staff[4].name = "Ms Elderberry";
  75.             staff[4].sales = 250;
  76.             staff[4].calculation_algorithm = enhanced_bonus;
  77.  
  78.             // 计算所有 Employee 的奖金
  79.             foreach (Employee person in staff)
  80.                 PerformBonusCalculation(person);
  81.  
  82.             // 显示所有 Employee 的详细信息
  83.             foreach (Employee person in staff)
  84.                 DisplayPersonDetails(person);
  85.  
  86.  
  87.         }
  88.  
  89.         public static void PerformBonusCalculation(Employee person)
  90.         {
  91.  
  92.             // 此方法使用存储在 person 对象中的委托
  93.             // 来进行计算。
  94.             // 注意:此方法能够识别乘数局部变量,尽管
  95.             // 该变量在此方法的范围之外。
  96.             //该乘数变量是一个“捕获的外部变量”。
  97.             person.bonus = person.calculation_algorithm(person.sales);
  98.         }
  99.  
  100.         public static void DisplayPersonDetails(Employee person)
  101.         {
  102.             Console.WriteLine(person.name);
  103.             Console.WriteLine(person.bonus);
  104.             Console.WriteLine("---------------");
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110. //csharp/4902

回复 "C#中委托用法演示代码"

这儿你可以回复上面这条便签

captcha