2

I have a list:

private List<OutlinedObject> allObjects = new List<OutlinedObject>();

that contains elements with a public method

Pulse(string a, int b) {}

and I want to call methods from all elements in array not iteratively. I think it should be like this, but it doesn't work:

allObjects.Pulse("prop", 10);

Can I do this in C#? Is it possible to do this without using delegates?

2
  • Do you wanna call them all at the same time? Commented Apr 11, 2021 at 19:20
  • @Andre.Santarosa Yes (if it possible) Commented Apr 11, 2021 at 19:21

1 Answer 1

2

If I understood you correctly you can use List<T>.ForEach (which actually just iterates the collection executing corresponding action sequentially):

allObjects.ForEach(i => i.Pulse("prop", 10));

If you want to run them in parallel you can use AsParallel (use WithDegreeOfParallelism to control number of parallel executions):

allObjects.AsParallel().ForAll(i => i.Pulse("prop", 10))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.