Arbitrary-sized string arrays in C#
I want to define an array of strin开发者_高级运维gs but I don't know the number of strings I'll need to store in it. Can I define such an array, and how can I insert strings into it?
Better use List like this:
List<string> names = new List<string>();
names.Add(name); ///whatever string you want to insert
Later if you need array of names, call:
string[] arr = names.ToArray();
If you have to use an array of strings then you should know the size upfront. If you don't know the size, then you can initialize array of some default length (say 10). The things that you have to do are:
- Keep the count of strings already added in array
- If it reaches the default length, you have re-initialize the array with a bigger length (say 15) and copy all existing strings to this new array.
- You have to keep checks of the boundaries of this array, you don't want to read from indexes you haven't used yet (i.e. if the index is greater then count)
So its better to use list rather then doing all this stuff by yourself
You can use a List<string>
, this will expand as you add items to it.
List<string> myList = new List<string>();
myList.Add("string1");
myList.Add("string2");
It can easily be converted to an array if needed:
string[] stringArray = myList.ToArray();
If you don't know the exact number of items you will need, an array may not be a good choice, as you will need to resize it (which is an expensive operation).
I would use the List class. You can add as much as you need without having to know how much you're going to put in there. If you do have some idea as to how much will be going in the list, you can use the capacity argument of List
's constructor to prevent performance problems.
int capacity = 3;
var listOfNames = List<string>(/* optional */ capacity);
listOfNames.Add("My Name 1");
listOfNames.Add("My Name 2");
listOfNames.Add("My Name 3");
var namesArray = listOfNames.ToArray();
I added the namesArray
line in there in case you really needed an array instead of a List for some reason.
See this page to see what all you can do with a List
.
You can use the following, as Oded mentioned above (or below), it will auto expand when items are added to it.
List<String> l = new List<String>();
l.Add("your string here");
...And if you'd like to iterate, then:
foreach(string i in l)
{
// Do something with each item (i);
}
...And to have an Array:
String[] a =
l.ToArray();
Use List<T>
. E.g.
var list = new List<string>();
list.Add("string1");
if you need the list to be an array, List<T>
has a ToArray()
method.
Internally List<T>
stores the data as an array of strings. When more space is required, it will allocate a new array of double the size of the current and copy all the references to the new array. I.e. you don't have to do anything, but if the code is performance critical, you may want to supply a default capacity when creating the list.
ArrayList will do the trick too.
ArrayList alist = new ArrayList();
alist.Add(String1); alist.Add(String2)
精彩评论