我环顾四周,但无法找到我的答案。如何初始化C#中的字符串数组列表?
我有这样的代码:
List<String[]> _myList = new List<String[]>();
我只想值初始化它,但我不知道怎么办。非常感谢
我环顾四周,但无法找到我的答案。如何初始化C#中的字符串数组列表?
我有这样的代码:
List<String[]> _myList = new List<String[]>();
我只想值初始化它,但我不知道怎么办。非常感谢
你可以这样说:
List<String[]> _myList = new List<String[]>()
{
new string[] { "string", "more string" },
new string[] { "and more string"},
};
或初始化之后添加这样的:
_myList.Add(new string[] {"add more string"});
试试这个:
List<String[]> _myList = new List<String[]>{new String[]{"a","b"},new String[]{"c","d"}};
这是List Initializer
语法。
你可以尝试这样的事:
List<String[]> _myList = new List<String[]> { new String[] { "a", "b", "c", "d"},
new String[] { "a", "b"},
new String[] { "b", "c"} };
这是集合初始化语法。欲了解更多信息,请看看here。
List<string[]> _myList = new List<string[]>() { new string[1] { "Cool" }, new string[2] { "Also", "cool" } };
首先,列表是不是在一个数组列表可以有可变数量的项目;另一方面,数组有固定数量的项目或成员,必须声明和遵守。
using System.Collections.Generic;
Class Program
{
Static Void Main(string[] args)
Var names = new List<string>
{
"Dave",
"Steve",
"Joe",
};
// you can get additional names added by using the Add keyword
names.Add("Hillary")
// If you want to print your list you can use the foreach loop
foreach(string name in names)
Console.Writeline(names);
}