what options do I have when initializing string[] object?
+1
A:
string[] str = new string[]{"1","2"};
string[] str = new string[4];
Mike Blandford
2009-10-01 16:07:55
+3
A:
You have several options:
string[] items = { "Item1", "Item2", "Item3", "Item4" };
string[] items = new string[]
{
"Item1", "Item2", "Item3", "Item4"
};
string[] items = new string[10];
items[0] = "Item1";
items[1] = "Item2"; // ...
Will Eddins
2009-10-01 16:08:27
Don't forget the `string[] items = { "Item1", "Item2", "Item3", "Item4" };` shortcut.
LukeH
2009-10-01 16:19:05
@Luke: Thanks, I indeed forgot about it.
Will Eddins
2009-10-01 16:20:36
+2
A:
Basic:
string[] myString = new string[]{"string1", "string2"};
or
string[] myString = new string[4];
myString[0] = "string1"; // etc.
Advanced: From a List
list<string> = new list<string>();
//... read this in from somewhere
string[] myString = list.ToArray();
From StringCollection
StringCollection sc = new StringCollection();
/// read in from file or something
string[] myString = sc.ToArray();
Oplopanax
2009-10-01 16:11:21