Posted by: Cirilo Meggiolaro | 11/7/2008

Tip of the day #24 – Anonymous methods

Anonymous methods provide a way to create a method body and pass its code as a parameter to a delegate instead of creating a delegate pointing to a specific method. Is it too complicated? Let’s check the code:

1. Our delegate

Let’s assume we have a delegate called Calculate that expects 3 parameters: 2 numbers to perform a calculation and an enumerator that determines the operation.

delegate void Calculate(decimal x, decimal y, MyOperationEnum operation);

public enum MyOperationEnum
{
Add,
Subtract,
Divide,
Multiply,
}

 
2. Using named method delegates

An instance of the delegate is create pointing the execution to a named method.

Calculate calc = new Calculate(MyNamedMethod);
calc(125, 321, MyOperationEnum.Add);

 
private decimal MyNamedMethod(decimal x, decimal y, MyOperationEnum operation)
{
/// Perform calculations
}

 
3. Using anonymous methods

A new instance of the delegate is created and instead of passing the method that will be triggered by the delegate, the implementation of that method is passed as parameter. The sequence is straightforward:

Calculate calc = delegate(decimal x, decimal y, MyOperationEnum operation)
{
switch (operation)
{
case MyOperationEnum.Add:
Console.WriteLine(“Result (Add): {0}”, x + y);
break;
case MyOperationEnum.Subtract:
Console.WriteLine(“Result (Subtract): {0}”, x – y);
break;
case MyOperationEnum.Divide:
Console.WriteLine(“Result (Divide): {0}”, x / y);
break;
case MyOperationEnum.Multiply:
Console.WriteLine(“Result (Multiply): {0}”, x * y);
break;
default:
break;
}
};

calc(123, 321, MyOperationEnum.Add);
calc(125, 321, MyOperationEnum.Subtract);
calc(125, 321, MyOperationEnum.Multiply);
calc(125, 321, MyOperationEnum.Divide);

Advertisements

Responses

  1. Awesome Cirilo! Keep going!

  2. Or how about:

    Calculate calc = delegate(MyOperationEnum operation)
    {
    switch (operation)
    {
    case MyOperationEnum.Add:
    return (x,y) => x + y;
    break;
    case MyOperationEnum.Subtract:
    return (x,y) => x – y;
    break;
    case MyOperationEnum.Divide:
    return (x,y) => x / y;
    break;
    case MyOperationEnum.Multiply:
    return (x,y) => x * y;
    break;
    default:
    break;
    }
    };

    calc(MyOperationEnum.Add)(123, 321);
    calc(MyOperationEnum.Subtract)(125, 321);
    calc(MyOperationEnum.Multiply)(125, 321);
    calc(MyOperationEnum.Divide)(125, 321);

  3. obviously you need to redefine the Calculate delegate to make the example above work:

    delegate void CalculateOp(decimal x, decimal y);
    delegate CalculateOp Calculate(MyOperationEnum operation);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: