array of string with unknown size

C#Arrays

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

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

Solution 6 - C#

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.

Solution 7 - C#

I suppose that the array size if a computed value.

int size = ComputeArraySize();

// Then

String[] array = new String[size]; 

Solution 8 - C#

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

Solution 9 - C#

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

Solution 10 - C#

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

Solution 11 - C#

string[ ] array = {};

// it is not null instead it is empty.

Solution 12 - C#

string foo = "Apple, Plum, Cherry";

string[] myArr = null;

myArr = foo.Split(',');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioneriksv88View Question on Stackoverflow
Solution 1 - C#Stan R.View Answer on Stackoverflow
Solution 2 - C#pmarfleeView Answer on Stackoverflow
Solution 3 - C#sepp2kView Answer on Stackoverflow
Solution 4 - C#Andrew HareView Answer on Stackoverflow
Solution 5 - C#Ravi RajinduView Answer on Stackoverflow
Solution 6 - C#jasonhView Answer on Stackoverflow
Solution 7 - C#VitaliyView Answer on Stackoverflow
Solution 8 - C#Wil PView Answer on Stackoverflow
Solution 9 - C#MalfistView Answer on Stackoverflow
Solution 10 - C#shreeshaView Answer on Stackoverflow
Solution 11 - C#DaminiView Answer on Stackoverflow
Solution 12 - C#KayView Answer on Stackoverflow