Array string initialization options

What parameters do I have when initializing the string[] object?

+43
arrays initialization c #
01 Oct '09 at 16:05
source share
4 answers

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"; // ... 
+73
01 Oct '09 at 16:08
source share

Basic:

 string[] myString = new string[]{"string1", "string2"}; 

or

 string[] myString = new string[4]; myString[0] = "string1"; // etc. 

Optional: From the 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(); 
+7
01 Oct '09 at 16:11
source share
 string[] str = new string[]{"1","2"}; string[] str = new string[4]; 
+2
Oct 01 '09 at 16:07
source share
+2
01 Oct '09 at 16:08
source share



All Articles