58

How is an array of strings where you do not know the array size in C#/.NET declared?

String[] array = new String[]; // this does not work
2

14 Answers 14

101

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

Sign up to request clarification or add additional context in comments.

8 Comments

Will they they be added after the order I put those in, so I can call them like array [int] ...
they will be added in the order you add them in, you can access them with an indexer. Edited answer for an example.
Yes, you can still index the items in a generic list the same way you use an array.
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.
Or am I just thinking of the sort order?
|
25

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];

Comments

9

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

Comments

7

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();

Comments

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

Comments

0

I suppose that the array size if a computed value.

int size = ComputeArraySize();

// Then

String[] array = new String[size]; 

Comments

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?

Comments

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];

Comments

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];

1 Comment

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
-1

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.

3 Comments

why would he be looking for StringBuilder? this doesn't make sense
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.
"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.
-1

string[ ] array = {};

// it is not null instead it is empty.

Comments

-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>();

Comments

-1

You should use a List<>. But you can do the following:

string[] strings = { };

strings = strings.Append("Hello").ToArray();
strings = strings.Append("world").ToArray();
strings = strings.Append("!").ToArray();

Console.WriteLine($"{strings[0]} {strings[1]} {strings[2]}");
//Prints: Hello world!

Although it works you really should use other dynamically scaling data structure. Only use arrays when the size is known beforehand.

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
-2

string foo = "Apple, Plum, Cherry";

string[] myArr = null;

myArr = foo.Split(',');

1 Comment

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! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.