2009-10-01 14 views

Odpowiedz

79

Masz kilka opcji:

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"; // ... 
+5

Nie zapomnij 'string [] items = { "Pozycja1", "Pozycja2", "Pozycja 3 Wartość", "ITEM4" }; 'skrót. – LukeH

+0

@Luke: Dzięki, naprawdę o tym zapomniałem. –

2
string[] str = new string[]{"1","2"}; 
string[] str = new string[4]; 
7

podstawowe:

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

lub

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

Advanced: z listy

list<string> = new list<string>(); 
//... read this in from somewhere 
string[] myString = list.ToArray(); 

Od StringCollection

StringCollection sc = new StringCollection(); 
/// read in from file or something 
string[] myString = sc.ToArray(); 
+0

Czy ktoś może dodać przykład "Z listy" powyżej, ale w przypadku ładowania zawartości 2 list do tablicy dwuwymiarowej (nie postrzępiony, czyli podwójny [,])? – skinnedKnuckles