It’s probably a really basic question, but I can’t find the answer anywhere.
I’m trying to loop through the input and put the results into an array using C#. From what I read the array has to have the number of elements set first.
Is there a way to just loop through and have the array number of elements dependent on the number of inputs?
TIA
Use a List object so that you can add the results of each iteration in your loop to the List until you have processed all of the input. Then you won’t have to keep track of indices / sizes of the array.
The
Listclass has a ToArray() method that you can use after the loop, if you want to store the results in an array. You can either return the array from your method, or clear out the List object after converting it to an array, in order to keep from having extra copies of the data lying around.