C# 初始化字符串数组的选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1504871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:13:54 来源:igfitidea点击:
Options for initializing a string array
提问by mrblah
What options do I have when initializing string[]
object?
初始化string[]
对象时有哪些选项?
采纳答案by Will Eddins
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"; // ...
回答by Mike Blandford
string[] str = new string[]{"1","2"};
string[] str = new string[4];
回答by itsmatt
回答by Blue Toque
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();