Search code examples
c#functionoverhead

Pass function as parameters vs setting a variable and passing it


I bet this question has been asked before, but I can't really find out what i'm looking for, so excuse me in advance :)

is there a difference (programmatically speaking OR overhead speaking) between this:

 var data = GetProducts();
 GetAllData(data);

and this:

GetAllData(GetProducts());

what are the pros and cons of both methods if any? is there a more elegant/right way of achieving it (say Func<>)?

thanks in advance, Rotem


Solution

  • Doing it in two lines makes it easier to debug, because you can break on the second line and observe the value assigned on the first line.

    The compiler will optimize them both into the same CIL anyway, so it's not a matter of efficiency. It's all a matter of preference.