Anonymous Method / Delegate

Delegates in C# are similar 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)  
            //Display the details of all Employees 
            foreach (Withdrawal muenzen in geld)  

    public static void DisplayCharges(Withdrawal muenzen)  
            WriteLine($"AccountOwnerName:  {muenzen.AccountOwnerName}");
            WriteLine($"Withdrawal Amount: {muenzen.withdrawal}");
            WriteLine($"Charge Amount:     {muenzen.charge}");

Download the source here.

Leave a Comment

Your email address will not be published.