56

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
eriksv88
  • 3,482
  • 3
  • 31
  • 50
  • possible duplicate of [How to add a string to a string\[\] array? There's no .Add function](http://stackoverflow.com/questions/1440265/how-to-add-a-string-to-a-string-array-theres-no-add-function) – Antonio Sep 04 '15 at 15:33
  • possible duplicate and good answer found here : https://stackoverflow.com/questions/53441268/still-learning-system-stackoverflowexception-was-thrown – m3.b Jan 15 '22 at 02:00

13 Answers13

95

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.
  • 15,757
  • 4
  • 50
  • 58
  • Will they they be added after the order I put those in, so I can call them like array [int] ... – eriksv88 Oct 21 '09 at 21:09
  • 1
    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. Oct 21 '09 at 21:11
  • 1
    Yes, you can still index the items in a generic list the same way you use an array. – David Oct 21 '09 at 21:11
  • 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 Oct 21 '09 at 21:21
  • Or am I just thinking of the sort order? – jasonh Oct 21 '09 at 21:22
  • Better use var array = new List(); – Incognito Jun 15 '13 at 18:26
  • If you are profiling don't use lists, they are very bad at performance. Since NET6 you can do it declaring an empty array. – Leandro Bardelli Mar 12 '23 at 04:52
26

You don't 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];
Mark
  • 21,067
  • 14
  • 53
  • 71
pmarflee
  • 3,428
  • 20
  • 21
9

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675
6

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
  • 344,730
  • 71
  • 640
  • 635
6

you can declare an empty array like below

String[] arr = new String[]{}; // declare an empty array
String[] arr2 = {"A", "B"}; // declare and assign values to an array
arr = arr2; // assign valued array to empty array

you can't assign values to above empty array like below

arr[0] = "A"; // you can't do this
Ravi Rajindu
  • 380
  • 2
  • 6
  • 23
0

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
  • 29,297
  • 11
  • 59
  • 61
  • why would he be looking for StringBuilder? this doesn't make sense – Stan R. Oct 21 '09 at 21:03
  • 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 Oct 21 '09 at 21:06
  • 1
    "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 Oct 21 '09 at 21:12
0

I suppose that the array size if a computed value.

int size = ComputeArraySize();

// Then

String[] array = new String[size]; 
Vitaliy
  • 8,044
  • 7
  • 38
  • 66
0

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
  • 3,341
  • 1
  • 20
  • 20
0

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
  • 31,179
  • 61
  • 182
  • 269
0

If you want to use array without knowing the size first you have to declare it and later you can instantiate it like

string[] myArray;
...
...
myArray=new string[someItems.count];
shreesha
  • 1,811
  • 2
  • 21
  • 30
  • 1
    Have you seen that this has an answer! And when this was created? If you try to get points, so I advise you to look at new items – eriksv88 Mar 02 '15 at 11:19
-1

string[ ] array = {};

// it is not null instead it is empty.

Damini
  • 395
  • 3
  • 5
-1

Meanwhile accepted answer is right, is not the answer to the question.
List are very bad at performance, maybe you should use:

String[] a = Array.Empty<string>();
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
-2

string foo = "Apple, Plum, Cherry";

string[] myArr = null;

myArr = foo.Split(',');

Kay
  • 1
  • Not good to convert a string like this! This is not so. NET split(Char()) is supposed to do! What if the input comes from a user?? You should check first for ","?? And if you hardcoded a string then you know the size anyway! :) – eriksv88 Sep 20 '13 at 12:18