ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-AnonymousDelegates(匿名委托) |
1.A,示例(Sample) 返回頂部 |
本示例演示了如何使用匿名委托來計算員工的薪水獎金。使用匿名委托簡化了程序,因為無需再定義一個單獨的方法。
每個雇員的數(shù)據(jù)都存儲在一個對象中,該對象包含個人詳細信息和一個引用計算獎金所需的算法的委托。通過以委托的方式定義算法,可以使用相同的方法來執(zhí)行獎金計算,而與實際計算方式無關(guān)。另外需要注意的是,一個局部變量 multiplier 變成了已捕獲的外部變量,因為它在委托計算中被引用了。
安全說明 |
---|
提供此代碼示例是為了闡釋一個概念,它并不代表最安全的編碼實踐,因此不應(yīng)在應(yīng)用程序或網(wǎng)站中使用此代碼示例。對于因?qū)⒋舜a示例用于其他用途而出現(xiàn)的偶然或必然的損害,Microsoft 不承擔(dān)任何責(zé)任。 |
在“調(diào)試”菜單上,單擊“開始執(zhí)行(不調(diào)試)”。
使用“更改目錄(cd)”命令轉(zhuǎn)到“AnonymousDelegates”目錄。
鍵入以下命令:
csc AnonymousDelegates.csAnonymousDelegates |
1.B,示例代碼(Sample Code)返回頂部 |
1.B.1,AnonymousDelegates.cs
// 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。// 此代碼的發(fā)布遵從// Microsoft 公共許可(MS-PL,http://opensource.org/licenses/ms-pl.html)的條款。////版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。using System;using System.Collections.Generic;using System.Text;namespace AnonymousDelegate_Sample{ // 定義委托方法。 delegate decimal CalculateBonus(decimal sales); // 定義一個 Employee 類型。 class Employee { public string name; public decimal sales; public decimal bonus; public CalculateBonus calculation_algorithm; } class PRogram { // 此類將定義兩個執(zhí)行計算的委托。 // 第一個是命名方法,第二個是匿名委托。 // 首先是命名方法。 // 該方法定義“獎金計算”算法的一個可能實現(xiàn)。 static decimal CalculateStandardBonus(decimal sales) { return sales / 10; } static void Main(string[] args) { // 獎金計算中用到的值。 // 注意: 此局部變量將變?yōu)?ldquo;捕獲的外部變量”。 decimal multiplier = 2; // 將此委托定義為命名方法。 CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus); // 此委托是匿名的,沒有命名方法。 // 它定義了一個備選的獎金計算算法。 CalculateBonus enhanced_bonus = delegate(decimal sales) { return multiplier * sales / 10; }; // 聲明一些 Employee 對象。 Employee[] staff = new Employee[5]; // 填充 Employees 數(shù)組。 for (int i = 0; i < 5; i++) staff[i] = new Employee(); // 將初始值賦給 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; // 計算所有 Employee 的獎金 foreach (Employee person in staff) PerformBonusCalculation(person); // 顯示所有 Employee 的詳細信息 foreach (Employee person in staff) DisplayPersonDetails(person); } public static void PerformBonusCalculation(Employee person) { // 此方法使用存儲在 person 對象中的委托 // 來進行計算。 // 注意: 此方法能夠識別乘數(shù)局部變量,盡管 // 該變量在此方法的范圍之外。 //該乘數(shù)變量是一個“捕獲的外部變量”。 person.bonus = person.calculation_algorithm(person.sales); } public static void DisplayPersonDetails(Employee person) { Console.WriteLine(person.name); Console.WriteLine(person.bonus); Console.WriteLine("---------------"); } }}View Code
1.B.2,
1.C,下載地址(Free Download)返回頂部 |
![]() | 作者:ylbtech出處:http://ylbtech.VEVb.com/本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。 |
新聞熱點
疑難解答