| |
|
Anonymous Method / Delegate
|
| |
|
Delegates in C# are similiar to function pointers in C++ and
allow methods to be passed as parameters.
|
| |
|
In this example we will create a delegate called CalculateCharge and a Withdrawal class:
|
| |
delegate decimal CalculateCharge(decimal
withdrawal);
class Withdrawal
{
public string
AccountOwnerName;
public decimal withdrawal;
public decimal charge;
public CalculateCharge
calculation;
}
|
| |
Our Program class will create 2 delegates to execute the calculation. The first
delegate will be named and the second one will be anonymous.
The first method is
named and called CalculateStandardCharge. It will multiply the amount withdrawn
by 1% and return that value as the Service Charge.
|
| |
|
The next method, ServiceChargeCalculation will use the delegate stored in the
Withdrawal object to perform the calculation.
|
| |
|
In our Main method we will set a risk factor which increases the amount of the
charge, for example, we could increase the charge based on the amount withdrawn.
NOTE: Within the ServiceChargeCalculation method the risk factor is known even
though the variable is outside the scope of the method. Search
for 'captured Outer Variable' for more information about this term.
|
| |
The standard_charge delegate is an example of a named method.
The highRisk_charge delegate is an example of a anoymous method and describes an
alternate calculation method.
|
| |
|
The first foreach statement (Calculate the charge for all Withdrawals) calculates
the charge using the method provided when we populated the Withdrawls class.
Either the standard_charge delegate or the highRisk_charge delegate.
The program thereby knows which function to use at runtime, I.e. we pass
the method/delegate as a parameter.
|
| |
|
Lastly, we display the output on the console. The same could eaisly be
stored in a database or logged in a flat file for later backend processing.
|
| |
class Program
{
static decimal
CalculateStandardCharge(decimal withdrawal)
{
return (withdrawal * .01m); //a charge of 1% is
levied
}
public static void
ServiceChargeCalculation(Withdrawal money)
{
money.charge =
money.calculation(money.withdrawal);
}
static void Main(string[]
args)
{
decimal riskFactor = .02m;
CalculateCharge
standard_charge = new
CalculateCharge(CalculateStandardCharge);
CalculateCharge
highRisk_charge = delegate(decimal
withdrawal) { return withdrawal * riskFactor; };
//
Let#s create some widrawls and populate them
Withdrawal[] geld =
new Withdrawal[3];
for
(int i = 0; i < 3; i++)
geld[i] = new Withdrawal();
geld[0].AccountOwnerName
= "Mr Jones";
geld[0].withdrawal = 20;
geld[0].calculation =
standard_charge;
geld[1].AccountOwnerName
= "Ms Rigby";
geld[1].withdrawal = 200;
geld[1].calculation =
standard_charge;
geld[2].AccountOwnerName
= "Mr Smith";
geld[2].withdrawal =
2000;
geld[2].calculation =
highRisk_charge;
//
Calculate the charge for all Withdrawals
foreach (Withdrawal muenzen in geld)
ServiceChargeCalculation(muenzen);
//
Display the details of all Employees
foreach (Withdrawal muenzen in geld)
DisplayCharges(muenzen);
Console.ReadLine();
}
public static void
DisplayCharges(Withdrawal muenzen)
{
Console.WriteLine(muenzen.AccountOwnerName);
Console.WriteLine(muenzen.charge);
Console.WriteLine("********************");
}
}
|
| |
| Download the source |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
| Posts: 113 |
| Comments:
86 |
| Fundamentals:
16 |
| |
 |
| |
| |
|
| |
 |
| |
 |
| |
 |
| |
|
| 2011 December (2) |
| 2011 November (6) |
| 2011 October (7) |
| 2011 September (7) |
| 2011 August (9) |
| 2011 July (9) |
| 2011 June (8) |
| 2011 May (9) |
| 2011 April (7) |
| 2011 March (9) |
| 2011 February (8) |
| 2011 January (8) |
| 2010 December (7) |
| 2010 November (8) |
| 2010 October (4) |
| |
| |
|
| |
|
|
| |
| |
|
The sample code on this website is provided to illustrate a concept and should not be used in
applications or Web sites without proper professional consultation, as it may not illustrate
the safest coding practices. I assume no liability for incidental or consequential damages
should the sample code be used for purposes other than as intended.
|
| |
|
| | | |