0

Lets have:

List<double> inputs,outputs;

Is any difference between this:

inputs.Clear();
outputs.ForEach(inputs.Add);

and this:

inputs.Clear();  
outputs.ForEach(x => inputs.Add(x));

1) I assume that both options will first clear list of inputs and than take all values in list of outputs and put them into list of inputs.
2) Both options looks like they are equivalent. Am I right? Is any difference between them?

user1097772
  • 3,499
  • 15
  • 59
  • 95

1 Answers1

2

No, there's no difference. They'll most likely be compiled to the same IL code.

However, in my opinion, this form

inputs.Clear();  
outputs.ForEach(x => inputs.Add(x));

shows your intent more clearly. I probably would avoid this altogether though and just use a foreach loop to add each element to inputs. But it ultimately comes down to your personal preference.

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • 1
    If you are using multiple ForEach'es in your code, it would help in terms of readability to be consistent in using one or the other. – Robert Columbia Jul 17 '16 at 22:07
  • @RobertColumbia Yeah good point. But I'm not gonna to mix it. I'm just wanted know the behaviour of that constructions, because they are new to me. – user1097772 Jul 17 '16 at 22:16