0

I know how to create a list of Controls and add new instances of them to it:

private List<FirstCircleControl> Circles = new List<FirstCircleControl>();
FirstCircleControl mc = new FirstCircleControl();
Circles.Add(mc);

I want to add a whole bunch of "FirstCircleControls". How would I add 10 controls to my list? I want to be able to "create" and then "add" them to the list using a loop.

1 Answer 1

1

I wonder why you might need to create them all at once and then add them to the list, but here's a solution:

Enumerable.Range(0, 10)
          .Select(x => new FirstCircleControl())
          .ToList()                        // Forces creation of controls.
          .ForEach(x => Circles.Add(x));   // Adds them to the list.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mehrdad. Enumerable looks super handy.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.