【发布时间】:2010-12-03 01:20:50
【问题描述】:
初始化string[]对象时有哪些选项?
【问题讨论】:
标签: c# arrays initialization
初始化string[]对象时有哪些选项?
【问题讨论】:
标签: c# arrays initialization
您有多种选择:
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"; // ...
【讨论】:
string[] items = { "Item1", "Item2", "Item3", "Item4" }; 快捷方式。
基本:
string[] myString = new string[]{"string1", "string2"};
或
string[] myString = new string[4];
myString[0] = "string1"; // etc.
高级: 来自列表
list<string> = new list<string>();
//... read this in from somewhere
string[] myString = list.ToArray();
来自字符串集合
StringCollection sc = new StringCollection();
/// read in from file or something
string[] myString = sc.ToArray();
【讨论】:
string[] str = new string[]{"1","2"};
string[] str = new string[4];
【讨论】: