1

Say that I have this line of code:

Action<int> square = number => Console.WriteLine(number * number);

Though I understand that it is handy to create a method on the fly, if you only need to call the lamda expression once, does it gain any perfomance by doing so?

In addition, let's say I do something more with the value before the WriteLine:

Action<int> square = number =>   {
    number += 10;
    Console.WriteLine(number * number);
};

I could just solve this very simple by adding 10 to number and then do number * number without the lambda expression. Can anyone give me a good example/situation when to use this expression?

Mark C.
  • 6,332
  • 4
  • 35
  • 71
  • 1
    "Can anyone give me a good example/situation when to use this expression?" - No, your lamda expression does not make sense to use – Binkan Salaryman May 08 '15 at 11:56
  • 2
    Usually you create methods to simplify your code and give some meaningful name to some functionality. It does not matter how many times you call method. Creating inline methods always makes code more complicated and harder to understand and maintain – Sergey Berezovskiy May 08 '15 at 11:57
  • 2
    Developers are mostly tends to use Action delegate as callback with async paradigm. – Jenish Rabadiya May 08 '15 at 11:58
  • 2
    @JenishRabadiya Even without the async paradigm. – xanatos May 08 '15 at 11:59
  • I'll add a use case: Lamda expressions can be used as handy shortcuts: ``Action log = Console.WriteLine;`` (notice that ``log`` calls the overloaded variant of WriteLine which does **not** format the message) – Binkan Salaryman May 08 '15 at 12:03
  • "does it gain any perfomance by doing so" - gain, compared to what? – O. R. Mapper May 08 '15 at 12:07
  • @O.R.Mapper Compared to manual ``inline``d versions of the lambda, I'd say – Binkan Salaryman May 08 '15 at 12:08
  • 1
    @BinkanSalaryman: Aha. That interpretation confirms my duplicate vote :) (In other words, this is actually about when and why to use a method pointer rather than calling some code the conventional way.) – O. R. Mapper May 08 '15 at 12:09

2 Answers2

2

I understand that it is handy to create a method on the fly, using lambda, if you only need to call it once but do I gain any perfomance by doing so?

Delegates (such as instances of Action<T>) are one level of indirection (very similar to virtual method dispatch); the runtime does not just execute the code inside the method when you invoke the delegate; the runtime will first have to figure out what method they reference, then invoke that method.

Delegates certainly don't speed things up. But then you should state what we are comparing them against.

That being said, the minute overhead of delegate dispatch is generally negligible in all but the most extreme scenarios; don't worry too much about the performance of delegate invocation.

Action<int> square = number => 
{
    number += 10;
    Console.WriteLine(number * number);
};

Can anyone give me a good example/situation when to use this expression?

I would not mutate arguments if I could just as well write the code another way; all the more so when we are talking about a lambda's parameter. I would instead put the value derived from the argument into a (temporary) local variable:

Action<int> square = number => 
{
    int n = number + 10;
    Console.WriteLine(n * n);
};

The code is not really any longer, and generally easier to understand because you don't have to worry about your lambda accidentally changing an object passed to it.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
0

Imagine the following:
You have list of values where you want to apply a function. You can save this function in an Action and later apply this function in your code. E.g:

Predicate<int> isEven = number => number % 2 == 0;
List<int> numbers = Enumerable.Range(1,100);
var evenNumbers = numbers.Where(isEven);

This is more reusable and more readable than a foreach loop.

default
  • 11,485
  • 9
  • 66
  • 102
ckruczek
  • 2,361
  • 2
  • 20
  • 23