Question

I want to create an array and initialize it with for example for testing:

string[] myList= new string[]
{       "item1",
        "item2",        
}

If later I want to fill it from a csv file, will I be able to add any number of items to it ?

I don't want to use dynamic array because initialization syntax with .add method is not as convenient when I have to do it by hand.

Was it helpful?

Solution

Arrays have a fixed size, so you will not be able to add any amount of numbers to it. The closest thing you can do is to create an array that is large enough to store the amount of numbers you will be most likely generating but this is highly inefficient and also prone to errors if you generate more numbers than you have initially anticipated.

You will have to use a dynamic data structure such as an ArrayList, you might not find it convenient but it is much easier and makes you code look neater and more efficient.

OTHER TIPS

no , you can not add multiple items.

What is the problem with List< string>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top