The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Anonymous Method / Delegate in C#  
 
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
 
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
Comment posted: 11/22/2010 2:39:23 PM by Martin
 
Good post, I like your site.
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
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)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
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.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben