Ylbtech-LanguageSamples-AnonymousDelegates (anonymous delegation), anonymous Delegation
| Ylbtech-Microsoft-CSharpSamples: ylbtech-LanguageSamples-AnonymousDelegates (anonymous delegation) |
| 1. A, example (Sample)Back to Top |
Example of "anonymous delegation"
This example shows how to use an anonymous delegate to calculate the employee's salary and bonus. The use of anonymous delegation simplifies the program because there is no need to define a separate method.
Each employee's data is stored in an object that contains personal details and a delegate that references the algorithm required to calculate the bonus. By defining algorithms in delegated mode, you can use the same method to perform bonus calculation, regardless of the actual calculation method. Note that a local variable multiplier becomes a captured external variable because it is referenced in the delegate calculation.
| Security description |
This sample code is provided to illustrate a concept. It does not represent the most secure coding practice, so it should not be used in applications or websites. Microsoft is not liable for any accidental or inevitable damages caused by the use of this sample code for other purposes. |
Generate and run the "anonymous delegate" example in Visual Studio
Generate and run the "anonymous delegate" example from the command line
| 1. B. Sample Code)Back to Top |
1. B .1, AnonymousDelegates. cs
// Copyright (C) Microsoft Corporation. All rights reserved. // The publication of this Code complies with // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html . //// 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); // defines an Employee type. Class Employee {public string name; public decimal sales; public decimal bonus; public CalculateBonus calculation_algorithm;} class Program {// This class defines two computation delegates. // The first is the naming method, and the second is the anonymous delegate. // First, the naming method. // This method defines a possible implementation of the "bonus calculation" algorithm. Static decimal CalculateStandardBonus (decimal sales) {return sales/10;} static void Main (string [] args) {// value used in bonus calculation. // Note: This local variable is changed to "captured external variables ". Decimal multiplier = 2; // define this delegate as a naming method. CalculateBonus standard_bonus = new CalculateBonus (CalculateStandardBonus); // This delegate is anonymous and has no naming 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]; // fill in the Employees array. For (int I = 0; I <5; I ++) staff [I] = new Employee (); // assign the initial value 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; sta Ff [4]. name = "Ms Elderberry"; staff [4]. sales = 250; staff [4]. calculation_algorithm = enhanced_bonus; // calculate the bonus foreach (Employee person in staff) of all employees; // display the detailed information of all employees, foreach (Employee person in staff) displayPersonDetails (person);} public static void extends mbonuscalculation (Employee person) {// This method uses the delegate stored in the person object for computation. // Note: This method can recognize multiplier local variables, although // This variable is out of the scope of this method. // The Multiplier variable is an "captured external variable ". 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)Back to Top |
|
Author: ylbtech Source: http://ylbtech.cnblogs.com/ The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable. |