Lets say I have something like this:
MySomething element1 = new MySomething(stuff);
MySomething element2 = new MySomething(stuff);
MySomething element3 = new MySomething(stuff);
MySomething element4 = new MySomething(stuff);
MySomething element5 = new MySomething(stuff);
//and so on...
Now lets say I have a list or something somewhere and I would like to add all of the elements to the list. They I have to to do:
myList.add(element1);
myList.add(element2);
//and so on...
That would take long. And If I would have 100+ elements, well it takes forever ^^ Is there a faster way to add that much of created ‘elements’ into a list? I know
myList.add(new MySomething(stuff));
But let say, the elements were already created like here in example. How to add them in c# into a list faster? Thank you 🙂
Edit: ok and I have to say, the names are not always element1-100 its sometime element23, element454 and so on. The number after element is not always sequential. And yes they are all of the same type.
If they are all of the same type and you know how many you need just use a loop to create an array initially:
later, add all items to the list: