简体   繁体   中英

Usage list.ForEach in C#

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM