tags:

views:

746

answers:

8

How is an array of string where you do not know where the array size in c#.NET?

String[] array = new String[]; // this does not work
+7  A: 

Is there a specific reason why you need to use an array? If you don't know the size before hand you might want to use List<String>

List<String> list = new List<String>();

list.Add("Hello");
list.Add("world");
list.Add("!");

Console.WriteLine(list[2]);

Will give you an output of

!

MSDN - List(T) for more information

Stan R.
Will they they be added after the order I put those in, so I can call them like array [int] ...
sv88erik
they will be added in the order you add them in, you can access them with an indexer. Edited answer for an example.
Stan R.
Yes, you can still index the items in a generic list the same way you use an array.
David
thx a lot:D You must have a good day on!
sv88erik
no problem, check answer for updated info and a link
Stan R.
I thought (and I thought the MSDN documentation verifies this...) that the internal order of the List class is an implementation detail that is not guaranteed. If you need to ensure order, like an array, use a Queue.
jasonh
Or am I just thinking of the sort order?
jasonh
+2  A: 

You can't create an array without a size. You'd need to use a list for that.

sepp2k
+1  A: 

I think you may be looking for the StringBuilder class. If not, then the generic List class in string form:

List<string> myStringList = new List<string();
myStringList.Add("Test 1");
myStringList.Add("Test 2");

Or, if you need to be absolutely sure that the strings remain in order:

Queue<string> myStringInOriginalOrder = new Queue<string();
myStringInOriginalOrder.Enqueue("Testing...");
myStringInOriginalOrder.Enqueue("1...");
myStringInOriginalOrder.Enqueue("2...");
myStringInOriginalOrder.Enqueue("3...");

Remember, with the List class, the order of the items is an implementation detail and you are not guaranteed that they will stay in the same order you put them in.

jasonh
why would he be looking for StringBuilder? this doesn't make sense
Stan R.
It looks like he's trying to put together several strings. I added that he may want to use the List class if he wants to keep track of several strings. Also, the Queue class if he needs to retain order.
jasonh
"with the List class, the order of the items is an implementation detail and you are not guaranteed that they will stay in the same order you put them in." That's not correct. List preserves order: if you do a sequence of Adds, then iterate the list, the items will be in the order you added them. (Of course you can explicitly insert items in the middle of the list, sort the list, etc.; but things remain in order you asked for.) You're probably thinking of Dictionary, HashSet, etc.
itowlson
Yeah, my mistake.
jasonh
A: 

I suppose that the array size if a computed value.

int size = ComputeArraySize();

// Then

String[] array = new String[size];
Vitaliy
A: 

Can you use a List strings and then when you are done use strings.ToArray() to get the array of strings to work with?

Wil P
+2  A: 

As others have mentioned you can use a List<String> (which I agree would be a better choice). In the event that you need the String[] (to pass to an existing method that requires it for instance) you can always retrieve an array from the list (which is a copy of the List<T>'s inner array) like this:

String[] s = yourListOfString.ToArray();
Andrew Hare
A: 

If you will later know the length of the array you can create the initial array like this:

String[] array;

And later when you know the length you can finish initializing it like this

array = new String[42];
Malfist
+1  A: 

You have to specify the size of an array when you instantiate it.

You can still declare the array and instantiate it later. For instance:

string[] myArray;

...

myArray = new string[size];
pmarflee